We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a0ce24e commit 2481148Copy full SHA for 2481148
1329-sort-the-matrix-diagonally/1329. Sort the Matrix Diagonally.py
@@ -0,0 +1,19 @@
1
+from collections import defaultdict
2
+import heapq
3
+from typing import List
4
+
5
+class Solution:
6
+ def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
7
+ diagonals = defaultdict(list)
8
9
+ # Collect all elements of each diagonal
10
+ for i in range(len(mat)):
11
+ for j in range(len(mat[0])):
12
+ heapq.heappush(diagonals[i - j], mat[i][j])
13
14
+ # Put the sorted elements back into the matrix
15
16
17
+ mat[i][j] = heapq.heappop(diagonals[i - j])
18
19
+ return mat
0 commit comments