-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonal-sort.py
More file actions
27 lines (22 loc) · 868 Bytes
/
Copy pathdiagonal-sort.py
File metadata and controls
27 lines (22 loc) · 868 Bytes
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
# https://binarysearch.com/problems/Diagonal-Sort
class Solution:
def sort_diag(self, matrix, i):
cols, rows = len(matrix[0]), len(matrix)
arr = [matrix[j][i+j] for j in range(cols) if i+j < cols and j < rows]
arr.sort()
for j in range(cols):
if i+j < cols and j < rows:
matrix[j][i+j] = arr[j]
def sort_diag_2(self, matrix, i):
cols, rows = len(matrix[0]), len(matrix)
arr = [matrix[i+j][j] for j in range(rows) if i+j < rows and j < cols]
arr.sort()
for j in range(rows):
if i+j < rows and j < cols:
matrix[i+j][j] = arr[j]
def solve(self, matrix):
for i in range(len(matrix[0])):
self.sort_diag(matrix, i)
for i in range(1, len(matrix)):
self.sort_diag_2(matrix, i)
return matrix