Skip to content

Practice Sort Matrix by Diagonals

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3446
LeetCode Problem

Sort Matrix by Diagonals

Problem Statement

Sort each top-left-to-bottom-right diagonal of the square matrix grid independently. Diagonals starting in the first column, including the main diagonal, must be non-increasing when read down and right. Diagonals starting in the top row after the first cell must be non-decreasing in that direction. Return the resulting matrix.

Source: Sort Matrix by Diagonals on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[1,7,3],[9,8,2],[4,5,6]]
Output
[[8,2,3],[9,6,7],[4,5,1]]

# Example 2

Input
grid = [[0,1],[1,2]]
Output
[[2,1],[1,0]]

# Example 3

Input
grid = [[1]]
Output
[[1]]

Constraints

grid.length == grid[i].length == n
1 <= n <= 10
-10^5 <= grid[i][j] <= 10^5

Related LeetCode Problems

Practice more problems similar to Sort Matrix by Diagonals.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.