Puzzle Spree

Puzzle Spree

k-Sorted Matrix

Sorting a nearly ordered matrix

Kuai Yu's avatar
Kuai Yu
Feb 08, 2025
∙ Paid

Problem Statement

You have an m×n integer matrix M. Each row is k-sorted, meaning every element in that row is at most k positions away from where it would be if you sorted the row in non-decreasing order. Your goal is to rearrange all elements so that the matrix is sorted in row-major order—the smallest elements fill the first row left to right, then th…

Keep reading with a 7-day free trial

Subscribe to Puzzle Spree to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Puzzle Spree · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture