A mosaic is laid out as an R-by-C grid of integer tile shades. An artist wants each main diagonal band to run smoothly from dark to light. A main diagonal band is the set of cells that share the same value of (row index minus column index); each band runs from its top-left cell toward the bottom-right.
Within each band, sort the values in ascending order, placing the smallest value at the top-left end of the band and the largest at the bottom-right end. Cells in different bands do not mix.
Print the resulting grid.
Line 1: two integers R and C.
Next R lines: each contains C space-separated integers (one grid row).
R lines, each with C space-separated integers: the grid after every diagonal band is sorted ascending.
Example 1
Input
2 2 3 1 4 2
Expected
2 1 4 3
Explanation
The main band holds cells (0,0)=3 and (1,1)=2; sorted ascending it becomes 2 then 3, so (0,0)=2 and (1,1)=3. The single-cell bands 1 and 4 stay put, giving [[2,1],[4,3]].
Example 2
Input
1 3 5 2 9
Expected
5 2 9
Explanation
In a single row every band has just one cell, so nothing changes: 5 2 9.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →