forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diagonal-traverse-ii.py
41 lines (36 loc) · 1.09 KB
/
diagonal-traverse-ii.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
# Time: O(m * n)
# Space: O(m)
import itertools
import collections
class Solution(object):
def findDiagonalOrder(self, nums):
"""
:type nums: List[List[int]]
:rtype: List[int]
"""
result, dq, col = [], collections.deque(), 0
for i in xrange(len(nums)+max(itertools.imap(len, nums))-1):
new_dq = collections.deque()
if i < len(nums):
dq.appendleft((i, 0))
for r, c in dq:
result.append(nums[r][c])
if c+1 < len(nums[r]):
new_dq.append((r, c+1))
dq = new_dq
return result
# Time: O(m * n)
# Space: O(m * n)
class Solution2(object):
def findDiagonalOrder(self, nums):
"""
:type nums: List[List[int]]
:rtype: List[int]
"""
result = []
for r, row in enumerate(nums):
for c, num in enumerate(row):
if len(result) <= r+c:
result.append([])
result[r+c].append(num)
return [num for row in result for num in reversed(row)]