-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSort-the-Matrix-Diagonally.py
38 lines (38 loc) · 1.14 KB
/
Sort-the-Matrix-Diagonally.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#link:https://leetcode.com/problems/sort-the-matrix-diagonally/
class Solution:
def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
ans = []
for i in range (len(mat)):
ans.append([None]*len(mat[i]))
for i in range (len(mat)):
row = i
col = 0
temp = []
while row < len(mat) and col < len(mat[0]):
temp.append(mat[row][col])
row += 1
col += 1
temp.sort()
row = i
col = 0
p = 0
for k in range (len(temp)):
ans[row][col] = temp[k]
row += 1
col += 1
for i in range (1, len(mat[0])):
row = 0
col = i
temp = []
while row < len(mat) and col < len(mat[0]):
temp.append(mat[row][col])
row += 1
col += 1
temp.sort()
row = 0
col = i
for k in range (len(temp)):
ans[row][col] = temp[k]
row += 1
col += 1
return ans