Skip to content

Commit 8fe939f

Browse files
author
Partho Biswas
committed
Merge branch 'master' of github.com:partho-maple/coding-interview-gym
2 parents 7fabde9 + 39c2ac4 commit 8fe939f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ I have solved quite a number of problems from several topics. See the below tabl
171171
|60| [941. Valid Mountain Array](https://tinyurl.com/tgdqrrk) | [Python](https://tinyurl.com/wu6rdaw/941_Valid_Mountain_Array.py), [Swift](https://tinyurl.com/wuja3c4/941_Valid_Mountain_Array.swift) | | Easy | |
172172
|61| [731. My Calendar II](https://tinyurl.com/sde6smv) | [Python](https://tinyurl.com/wu6rdaw/731_My_Calendar_II.py), [Swift](https://tinyurl.com/wuja3c4/731_My_Calendar_II.swift) | | Medium | Merge interval |
173173
|62| [59. Spiral Matrix II](https://tinyurl.com/pqfjvm7) | [Python](https://tinyurl.com/wu6rdaw/59_Spiral_Matrix_II.py), [Swift](https://tinyurl.com/wuja3c4/59_Spiral_Matrix_II.swift) | | Medium | loved it |
174+
|63| **[525. Contiguous Array](https://tinyurl.com/txbs9wh)** | [Python](https://tinyurl.com/wu6rdaw/525_Contiguous_Array.py), [Swift](https://tinyurl.com/wuja3c4/525_Contiguous_Array.swift) | [Art 1](https://tinyurl.com/qmbd2vl) | Medium | loved it. Check again |
174175

175176

176177
</p>

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

0 commit comments

Comments
 (0)