

Computer the minimum number of jumps needed to reach each stage
Define Subproblem:
opt[i] = = minimum number of jumps to reach stage i
j = all previous indices that can directly reach iopt[i] = minimum of opt[j] + 1Initialization:
opt[0] = 0opt[i] = infinity for all iPseudocode:
for i = 1 to n-1:
for j = i-1 to 0:
if nums[j] >= i - j:
OPT[i] = min(OPT[i], OPT[j] + 1)
endfor
opt[n-1], the minimum jumps needed to reach the last indexclass Solution:
def jump(self, nums: List[int]) -> int:
n = len(nums)
# opt[i] = minimum number of jumps to reach stage i
opt = [float('inf')] * n
opt[0] = 0
for i in range(1, n):
for j in range(i, -1, -1):
if nums[j] >= i - j:
opt[i] = min(opt[i], opt[j] + 1)
return opt[n-1]
Worst Case Time Complexity:
i (from 1 to n-1) -> j < i -> Space Complexity: (DP array of size )
Traverse the array once, keeping track of:
end: the furthest index reachable with the current jump countfarthest: the furthest index reachable with one more jumpend = farthestclass Solution:
def jump(self, nums: List[int]) -> int:
n = len(nums)
if n <= 1:
return 0
jumps = 0
end = 0 # current range end
farthest = 0 # farthest reachable next
for i in range(n - 1):
farthest = max(farthest, i + nums[i])
if i == end: # end of current range
jumps += 1
end = farthest
if end >= n - 1: # last index covered
break
return jumps
jumps, end, farthest))