Skip to content

Commit 2481148

Browse files
committed
Runtime: 68 ms (69.15%), Memory: 17.1 MB (22.81%) - Gitcode
1 parent a0ce24e commit 2481148

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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+
for i in range(len(mat)):
16+
for j in range(len(mat[0])):
17+
mat[i][j] = heapq.heappop(diagonals[i - j])
18+
19+
return mat

0 commit comments

Comments
 (0)