Skip to content

Commit efb2f51

Browse files
authored
Create sort-the-matrix-diagonally.py
1 parent 9bfbd31 commit efb2f51

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python/sort-the-matrix-diagonally.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(m * n * log(min(m, n))
2+
# Space: O(m * n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def diagonalSort(self, mat):
9+
"""
10+
:type mat: List[List[int]]
11+
:rtype: List[List[int]]
12+
"""
13+
lookup = collections.defaultdict(list)
14+
for i in xrange(len(mat)):
15+
for j in xrange(len(mat[0])):
16+
lookup[i-j].append(mat[i][j])
17+
for v in lookup.itervalues():
18+
v.sort()
19+
for i in reversed(xrange(len(mat))):
20+
for j in reversed(xrange(len(mat[0]))):
21+
mat[i][j] = lookup[i-j].pop()
22+
return mat

0 commit comments

Comments
 (0)