forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zigzag_Traverse.py
37 lines (32 loc) · 990 Bytes
/
Zigzag_Traverse.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
# Time O(n) | Space O(n) >> where n is the total number of elements in 2D array
def zigzagTraverse(array):
height = len(array) - 1
width = len(array[0]) - 1
result = []
row, col = 0, 0
goingDown = True
while not isOutOfBound(height, width, row, col):
result.append(array[row][col])
if goingDown:
if col == 0 or row == height:
goingDown = False
if row == height:
col += 1
else:
row += 1
else:
row += 1
col -= 1
else:
if col == width or row == 0:
goingDown = True
if col == width:
row += 1
else:
col += 1
else:
row -= 1
col += 1
return result
def isOutOfBound(height, width, row, col):
return row < 0 or row > height or col < 0 or col > width