Skip to content

Commit 34821d1

Browse files
authored
Added solution for Jump game (55) using Dynamic Programming (codedecks-in#217)
* Added solution for Jump game (55) using Dynamic Programming * Edited README.md
1 parent 190cc17 commit 34821d1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Python/jumpGame.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# You are given an integer array nums. You are initially positioned at the array's first index,
2+
# and each element in the array represents your maximum jump length at that position.
3+
# Return true if you can reach the last index, or false otherwise.
4+
5+
# Input: nums = [2,3,1,1,4]
6+
# Output: true
7+
# Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
8+
9+
# Input: nums = [3,2,1,0,4]
10+
# Output: false
11+
# Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
12+
13+
'''
14+
Time Complexity: O(n),
15+
Space Complexity: O(n)
16+
'''
17+
18+
def canJump(nums):
19+
ptr1 = len(nums) - 1
20+
ptr2 = ptr1 - 1
21+
22+
while ptr2 >= 0:
23+
if nums[ptr2] >= ptr1 - ptr2:
24+
ptr1 = ptr2
25+
ptr2 -= 1
26+
else :
27+
ptr2 -= 1
28+
29+
if ptr1 == 0:
30+
return True
31+
else:
32+
return False
33+
34+
print(canJump[3,2,1,0,4])

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
394394
| 174 | [Dungeon Game](https://leetcode.com/problems/dungeon-game/) | [C++](./C++/dungeon-game.pp) | _O(M\*N)_ | _O(M\*N)_ | Hard | Dynamic Programming | |
395395
| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Java](./Java/climbing-stairs.java) | _O(N)_ | _O(1)_ | Easy | DP | |
396396
| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [C++](./C++/Count-Different-Palindromic-Subsequences.cpp) | _O(N\*N)_ | _O(N\*N)_ | Hard | DP | |
397+
| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Python](./Python/jumpGame.py) | _O(N)_ | _O(N)_ | Medium | DP | |
397398

398399
<br/>
399400
<div align="right">
@@ -510,6 +511,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
510511
| [Sachin_Upadhyay](https://github.com/sachsbu) <br> <img src="https://avatars.githubusercontent.com/u/24941685?v=4" width="100" height="100"> | India | Java | [GitHub](https://github.com/sachsbu)
511512
| [Amisha Sahu](https://github.com/Amisha328) <br> <img src = "https://avatars.githubusercontent.com/u/58816552?v=4" width="100" height="100"> | India | C++ | [CodeChef](https://www.codechef.com/users/amisha328)<br/>[LeetCode](https://leetcode.com/Mishi328/)<br/>[HackerRank](https://www.hackerrank.com/amishasahu328)
512513
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
514+
| [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage)
513515
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
514516
<div align="right">
515517
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)