From f5c0147443764cd9ad8755637663cd293613866a Mon Sep 17 00:00:00 2001 From: Won Joon Thomas Choi <113500771+724thomas@users.noreply.github.com> Date: Sat, 12 Sep 2026 20:49:24 +0900 Subject: [PATCH] Create 1329. Sort the Matrix Diagonally.py --- .../1329. Sort the Matrix Diagonally.py" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "leetcode3/\354\265\234\354\233\220\354\244\200/1329. Sort the Matrix Diagonally.py" diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/1329. Sort the Matrix Diagonally.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/1329. Sort the Matrix Diagonally.py" new file mode 100644 index 00000000..c3f282ea --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/1329. Sort the Matrix Diagonally.py" @@ -0,0 +1,32 @@ +class Solution: + def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]: + def helper(a, b): + nums = [] + row = a + col = b + while row < n and col < m: + nums.append(mat[row][col]) + row += 1 + col += 1 + nums.sort() + + row = a + col = b + idx = 0 + while row < n and col < m: + mat[row][col] = nums[idx] + row += 1 + col += 1 + idx += 1 + + n = len(mat) + m = len(mat[0]) + + for top in range(m): + helper(0, top) + + for left in range(1, n): + helper(left, 0) + + return mat +