55. Jump Game

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Input: nums = [3,2,1,0,4]
Output: false
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.
def canJump(self, nums: List[int]) -> bool:
goal = len(nums) - 1
for i in range(len(nums)-1, -1, -1):
if nums[i] >= goal - i:
goal = i
return True if goal==0 else False

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store