mediumGreedyPure DSA~20 min
Can The Pipeline Reach Its Final Stage
A deployment pipeline has stages laid out in a line. From stage i you can skip ahead by up to nums[i] stages (its max jump). Starting at stage 0, decide whether the final stage is reachable.
Problem
Given an integer array nums where nums[i] is the maximum forward jump length from index i, return true if you can reach the last index starting from index 0, otherwise false.
Input
An integer array nums of non-negative jump lengths.
Output
A boolean — true if the last index is reachable.
Constraints
- 1 ≤ n ≤ 1e4
- 0 ≤ nums[i] ≤ 1e5
Examples
Example 1
Input
[2,3,1,1,4]
Output
true
Jump 1 from index 0 to 1, then 3 from index 1 to the end.
Example 2
Input
[3,2,1,0,4]
Output
false
Every path lands on index 3 (value 0), a dead end before the last index.