-
Notifications
You must be signed in to change notification settings - Fork 14
/
1329-kir3i.py
49 lines (38 loc) · 1.25 KB
/
1329-kir3i.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
39
40
41
42
43
44
45
46
47
48
49
class Solution:
def diagonalSort(self, mat):
my = len(mat)
mx = len(mat[0])
matD = self.toDiagonal(mat, mx, my)
for line in matD:
line.sort()
return self.toRectagle(matD, mx, my)
def toDiagonal(self, mat, mx, my):
rtn = []
for y_base in range(my-1, 0, -1):
line = []
for i in range(mx):
if i+y_base >= my:
break
line.append(mat[i+y_base][i])
rtn.append(line) # make one diagonal line
for x_base in range(mx):
line = []
for i in range(my):
if i+x_base >= mx:
break
line.append(mat[i][i+x_base])
rtn.append(line)
return rtn
def toRectagle(self, matD, mx, my):
rtn = [[0]*mx for _ in range(my)]
for y_base in range(my-1, 0, -1):
for i in range(mx):
if i+y_base >= my:
break
rtn[i+y_base][i] = matD[my-1-y_base][i]
for x_base in range(mx):
for i in range(my):
if i+x_base >= mx:
break
rtn[i][i+x_base] = matD[my-1+x_base][i]
return rtn