Skip to content

Commit accdeea

Browse files
author
Joseph Luce
authored
Update 055_jump_game.md
1 parent 01b0e5a commit accdeea

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

leetcode/medium/055_jump_game.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
# 55. Jump Game
22

3+
## Dynamic Programming Top Down Solution
4+
5+
- Runtime: O(N^2)
6+
- Space: O(N)
7+
- N = Number of elements in array
8+
9+
You can see that the question is basically asking, given a certain index, can get we to the start or end, depending on which way you look at it.
10+
So in essence, you really only need to save a variable boolean for each index stating that its reachable or not.
11+
This requires an array of size N.
12+
Then its simply iterating each index and setting all reachable indexes to True, requiring two for loops.
13+
This will equate to O(N^2) run-time and O(N) space.
14+
15+
You can do this in a bottom up manner but it will just have to be in reverse.
16+
17+
```
18+
class Solution:
19+
def canJump(self, nums: List[int]) -> bool:
20+
if len(nums) == 0:
21+
return False
22+
dp = [False] * len(nums)
23+
dp[0] = True
24+
for start_index in range(0, len(nums)):
25+
if dp[start_index]:
26+
for jump_index in range(start_index, min(len(nums), start_index+nums[start_index]+1)):
27+
dp[jump_index] = True
28+
else:
29+
break
30+
return dp[-1]
31+
```
32+
333
## Best Solution
434
- Runtime: O(N)
535
- Space: O(1)

0 commit comments

Comments
 (0)