Skip to content

Commit e828d9f

Browse files
author
Partho Biswas
committed
55. Jump Game
1 parent 1e188a9 commit e828d9f

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ I have solved quite a number of problems from several topics. See the below tabl
280280
|04| [406. Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)| [Python](leetcode.com/python/406_Queue_Reconstruction_by_Height.py)| [Article 1](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89345/Easy-concept-with-PythonC%2B%2BJava-Solution), [Article 2](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89359/Explanation-of-the-neat-Sort%2BInsert-solution) | Medium | 📌 Fundamentals |
281281
|05| [621. Task Scheduler](https://leetcode.com/problems/task-scheduler/)| [Python](leetcode.com/python/621_Task_Scheduler.py)| [Ex 1](https://leetcode.com/problems/task-scheduler/discuss/190390/Can-anyone-explain-to-me-what-the-problem-is-asking), [Ex 2](https://leetcode.com/problems/task-scheduler/discuss/130786/Python-solution-with-detailed-explanation), [Vid 1](https://www.youtube.com/watch?v=hVhOeaONg1Y) | Medium | 📌 Extremely tricky. Not done. Check later |
282282
|06| [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/)| [Python](leetcode.com/python/392_Is_Subsequence.py)| [Ex 1](https://leetcode.com/problems/is-subsequence/discuss/87264/Easy-to-understand-binary-search-solution), [Ex 3](https://leetcode.com/problems/is-subsequence/discuss/87302/Binary-search-solution-for-follow-up-with-detailed-comments/144323) | Easy | 📌 |
283-
|07| [55. Jump Game](https://leetcode.com/problems/jump-game/)| [Python](leetcode.com/python/55_Jump_Game.py)| ```diff + [Text test]() ``` | Medium | 📌 |
283+
|07| *[55. Jump Game](https://leetcode.com/problems/jump-game/)* | [Python](leetcode.com/python/55_Jump_Game.py)| *[Official](https://leetcode.com/articles/jump-game/)* | Medium | 📌 *Must Check. Learned a lot* |
284284

285285

286286
### [Dynamic Programming](https://www.educative.io/courses/grokking-dynamic-programming-patterns-for-coding-interviews)

leetcode.com/python/55_Jump_Game.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class Solution(object):
2+
3+
# # Backtracking Approach - TLE
4+
# def canJump(self, nums):
5+
# """
6+
# :type nums: List[int]
7+
# :rtype: bool
8+
# """
9+
# return self.canJumpHelper(nums, 0)
10+
#
11+
# def canJumpHelper(self, nums, currentIdx):
12+
# if currentIdx == len(nums) - 1:
13+
# return True
14+
# nextMaxJumpIdx = min(currentIdx + nums[currentIdx], len(nums) - 1)
15+
# for i in range(nextMaxJumpIdx, currentIdx,
16+
# -1): # Python for loop decrementing index >> https://stackoverflow.com/questions/28650128/python-for-loop-decrementing-index/28650284
17+
# if self.canJumpHelper(nums, i):
18+
# return True
19+
# return False
20+
#
21+
# # DP top-down with memoization Approach - TLE
22+
# def canJump(self, nums):
23+
# """
24+
# :type nums: List[int]
25+
# :rtype: bool
26+
# """
27+
# memo = [0] * len(nums)
28+
# memo[-1] = True # Here True means reachable and True means not rechable
29+
# return self.canJumpHelper(nums, 0, memo)
30+
#
31+
# def canJumpHelper(self, nums, currentIdx, memo):
32+
# if memo[currentIdx] != 0:
33+
# return memo[currentIdx]
34+
# nextMaxJumpIdx = min(currentIdx + nums[currentIdx], len(nums) - 1)
35+
# for i in range(nextMaxJumpIdx, currentIdx, -1):
36+
# if self.canJumpHelper(nums, i, memo):
37+
# memo[currentIdx] = True
38+
# return True
39+
# memo[currentIdx] = False
40+
# return False
41+
#
42+
# # DP bottom-up with memoization Approach - TLE
43+
# def canJump(self, nums):
44+
# """
45+
# :type nums: List[int]
46+
# :rtype: bool
47+
# """
48+
# memo = [0] * len(nums)
49+
# memo[-1] = True # Here True means reachable and True means not rechable
50+
# for i in range(len(nums) - 1, -1, -1):
51+
# nextMaxJumpIdx = min(i + nums[i], len(nums) - 1)
52+
# for j in range(i + 1, nextMaxJumpIdx + 1):
53+
# if memo[j] is True:
54+
# memo[i] = True
55+
# break
56+
# return True if memo[0] == True else False
57+
58+
# Greedy
59+
def canJump(self, nums):
60+
"""
61+
:type nums: List[int]
62+
:rtype: bool
63+
"""
64+
lastPosition = len(nums) - 1
65+
for i in range(len(nums) - 1, -1, -1):
66+
nextMaxJump = i + nums[i]
67+
if nextMaxJump >= lastPosition:
68+
lastPosition = i
69+
return True if lastPosition == 0 else False
70+
71+
72+
sol = Solution()
73+
# input = [2,3,0,1,4]
74+
input = [3,2,1,0,4]
75+
# input = [2,0,6,9,8,4,5,0,8,9,1,2,9,6,8,8,0,6,3,1,2,2,1,2,6,5,3,1,2,2,6,4,2,4,3,0,0,0,3,8,2,4,0,1,2,0,1,4,6,5,8,0,7,9,3,4,6,6,5,8,9,3,4,3,7,0,4,9,0,9,8,4,3,0,7,7,1,9,1,9,4,9,0,1,9,5,7,7,1,5,8,2,8,2,6,8,2,2,7,5,1,7,9,6]
76+
output = sol.canJump(input)
77+
print('res: ', output)

0 commit comments

Comments
 (0)