- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/jump-game/
- topics: Dynamic Programming
You are given an integer array nums
. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true
if you can reach the last index, or false
otherwise.
class Solution:
def canJump(self, nums: List[int]) -> bool:
memo = {}
def jump(i):
nonlocal memo
if i not in memo:
if i < 0:
memo[i] = False
elif i == 0:
memo[i] = True
else:
memo[i] = False
for j in range(i-1, -1, -1):
memo[i] = (nums[j] >= (i-j) and jump(j))
if memo[i]:
break
return memo[i]
return jump(len(nums)-1)