All problems
Jump Game II
Problem
Given nums where nums[i] is the maximum jump length from index i, return the minimum number of jumps needed to reach the last index; assume the end is always reachable.
Use a greedy layer expansion: scan positions up to the current jump boundary while tracking the farthest reachable index, then jump when the boundary is crossed. A single-element list needs 0 jumps.
Hint
All positions within the current boundary cost the same number of jumps.
Test cases
case 1
in: [[2,3,1,1,4]]
expected: 2
case 2
in: [[2,3,0,1,4]]
expected: 2
case 3
in: [[0]]
expected: 0
case 4
in: [[1,1,1,1]]
expected: 3
case 5
in: [[5]]
expected: 0
Starter codePython
def min_jumps(nums):
# Your code here
pass5 test cases · run your code against them in the editor