3446. Sort Matrix by Diagonals Medium
1/**
2 * [3446] Sort Matrix by Diagonals
3 *
4 * You are given an n x n square matrix of integers grid. Return the matrix such that:
5 *
6 * The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.
7 * The diagonals in the top-right triangle are sorted in non-decreasing order.
8 *
9 *
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">grid = [[1,7,3],[9,8,2],[4,5,6]]</span>
13 * Output: <span class="example-io">[[8,2,3],[9,6,7],[4,5,1]]</span>
14 * Explanation:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2024/12/29/4052example1drawio.png" style="width: 461px; height: 181px;" />
16 * The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:
17 *
18 * [1, 8, 6] becomes [8, 6, 1].
19 * [9, 5] and [4] remain unchanged.
20 *
21 * The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:
22 *
23 * [7, 2] becomes [2, 7].
24 * [3] remains unchanged.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">grid = [[0,1],[1,2]]</span>
29 * Output: <span class="example-io">[[2,1],[1,0]]</span>
30 * Explanation:
31 * <img alt="" src="https://assets.leetcode.com/uploads/2024/12/29/4052example2adrawio.png" style="width: 383px; height: 141px;" />
32 * The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The other diagonals are already in the correct order.
33 * </div>
34 * <strong class="example">Example 3:
35 * <div class="example-block">
36 * Input: <span class="example-io">grid = [[1]]</span>
37 * Output: <span class="example-io">[[1]]</span>
38 * Explanation:
39 * Diagonals with exactly one element are already in order, so no changes are needed.
40 * </div>
41 *
42 * Constraints:
43 *
44 * grid.length == grid[i].length == n
45 * 1 <= n <= 10
46 * -10^5 <= grid[i][j] <= 10^5
47 *
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/sort-matrix-by-diagonals/
52// discuss: https://leetcode.com/problems/sort-matrix-by-diagonals/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57 pub fn sort_matrix(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
58 vec![]
59 }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_3446() {
70 }
71}
72Back
© 2026 bowen.ge All Rights Reserved.