Skip to content

Commit 1e4f6dd

Browse files
author
Partho Biswas
committed
no message
1 parent 4dac9d4 commit 1e4f6dd

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

algoexpert.io/python/Min_Number_Of_Jumps.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Solution #1
22
# O(n^2) time | O(n) space
33
def minNumberOfJumps(array):
4-
jumps = [float("inf") for x in array]
5-
jumps[0] = 0
6-
for i in range(1, len(array)):
7-
for j in range(0, i):
8-
if array[j] + j >= i :
9-
jumps[i] = min(jumps[i], jumps[j] + 1)
10-
return jumps[-1]
4+
aLen = len(array)
5+
if aLen == 1:
6+
return 0
7+
dp = [float("inf") for _ in range(aLen)]
8+
dp[0] = 0
9+
for destiinationIdx in range(1, aLen):
10+
for sourceIdx in range(0, destiinationIdx):
11+
if sourceIdx + array[sourceIdx] >= destiinationIdx:
12+
dp[destiinationIdx] = min(dp[destiinationIdx], dp[sourceIdx] + 1)
13+
return dp[-1]
1114

1215
# Solution #2
1316
# O(n) time | O(1) space

algoexpert.io/python/Zigzag_Traverse.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def zigzagTraverse(array):
77
result = []
88
row, col = 0, 0
99
goingDown = True
10-
while not isOutOfBounds(row, col, height, width):
10+
while not isOutOfBound(height, width, row, col):
1111
result.append(array[row][col])
1212
if goingDown:
1313
if col == 0 or row == height:
@@ -20,7 +20,7 @@ def zigzagTraverse(array):
2020
row += 1
2121
col -= 1
2222
else:
23-
if row == 0 or col == width:
23+
if col == width or row == 0:
2424
goingDown = True
2525
if col == width:
2626
row += 1
@@ -29,8 +29,9 @@ def zigzagTraverse(array):
2929
else:
3030
row -= 1
3131
col += 1
32+
3233
return result
3334

3435

35-
def isOutOfBounds(row, col, height, width):
36-
return row < 0 or row > height or col < 0 or col > width
36+
def isOutOfBound(height, width, row, col):
37+
return row < 0 or row > height or col < 0 or col > width

0 commit comments

Comments
 (0)