Skip to content

Commit 28e8628

Browse files
committed
Adding Efficient Solution for Problem - 55 - Jump Game
1 parent 0b1a845 commit 28e8628

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Jump_Game/EfficientSolution.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 55
6+
## Problem Name: Jump Game
7+
##===================================
8+
#
9+
#Given an array of non-negative integers, you are initially positioned at the first index of the array.
10+
#
11+
#Each element in the array represents your maximum jump length at that position.
12+
#
13+
#Determine if you are able to reach the last index.
14+
#
15+
#Example 1:
16+
#
17+
#Input: [2,3,1,1,4]
18+
#Output: true
19+
#Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
20+
#
21+
#Example 2:
22+
#
23+
#Input: [3,2,1,0,4]
24+
#Output: false
25+
#Explanation: You will always arrive at index 3 no matter what. Its maximum
26+
# jump length is 0, which makes it impossible to reach the last index.
27+
class Solution:
28+
def canJump(self, arr):
29+
curr = 0 #Initialize curr
30+
for i in range(len(arr)): #Loop through array
31+
if i > curr: #Condition-check: If index > curr
32+
return False #We return false
33+
curr = max(curr, i + arr[i]) #Update curr by taking max of curr and i + arr[i]
34+
return True #Otherwise return True

0 commit comments

Comments
 (0)