← DiffPush

Jump Game II

Standard Bar Greedy Approach · Medium O(n) · O(1)

The Relay Bidding Ladder

The same relay network now charges per hop, so the courier wants the minimum number of relays to reach the final post. Dispatch proceeds in waves: within the current wave's reachable band, scout the farthest post any member can open up; when the band is exhausted, that scout becomes the next wave's frontier and the hop counter rises by one.

Input: An integer array nums where nums[i] is the maximum hop length from index i.

Output: Return the minimum number of hops needed to reach the last index.

Constraints

Examples

Example 1

Input: {"nums":[2,3,1,1,4]}
Output: 2
One hop reaches index 1, whose 3-hop range lands on the last index.

Example 2

Input: {"nums":[2,3,0,1,4]}
Output: 2
A different second hop (through index 1 to index 4) still needs only two waves.

Solve this in your browser →

Also on LeetCode ↗