Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def canJump(self, nums: List[int]) -> bool:
- max_reach=0
- for i in range(len(nums)):
- 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.
- return False
- max_reach=max(i+nums[i],max_reach)
- 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.
Advertisement
Add Comment
Please, Sign In to add comment