imashutosh51

Possible to reach at the end of array of not(only forward move) /Jump Game

Jan 13th, 2026 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class Solution:
  2. def canJump(self, nums: List[int]) -> bool:
  3. max_reach=0
  4.  
  5. for i in range(len(nums)):
  6. if i>max_reach: #This handles the situation when you came to i but using all the elements before i can't make you reach to i for eg.[3,2,1,0,4] so when nums[i]==0 then i came ahead of max_reach and using 3,2 and 1, you can't reach 0.
  7. return False
  8. max_reach=max(i+nums[i],max_reach)
  9.  
  10. return True #When your max_reach can go till the last element or ahead of the last element, you can obvious reach the last element.
  11.  
Advertisement
Add Comment
Please, Sign In to add comment