Skip to content

Commit f5c0147

Browse files
authored
Create 1329. Sort the Matrix Diagonally.py
1 parent 67a0125 commit f5c0147

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
3+
def helper(a, b):
4+
nums = []
5+
row = a
6+
col = b
7+
while row < n and col < m:
8+
nums.append(mat[row][col])
9+
row += 1
10+
col += 1
11+
nums.sort()
12+
13+
row = a
14+
col = b
15+
idx = 0
16+
while row < n and col < m:
17+
mat[row][col] = nums[idx]
18+
row += 1
19+
col += 1
20+
idx += 1
21+
22+
n = len(mat)
23+
m = len(mat[0])
24+
25+
for top in range(m):
26+
helper(0, top)
27+
28+
for left in range(1, n):
29+
helper(left, 0)
30+
31+
return mat
32+

0 commit comments

Comments
 (0)