Skip to content
BytePatterns

Can the Last Index Be Reached

EasyGreedy#greedy#farthest-reach~15m

Problem

You start at index 0 of a list of non-negative integers. The value at each index is the longest jump you can make from there, so from index i you can land on any index from i + 1 to i + jumps[i]. Return True if you can reach the last index, otherwise False.

Examples

Input:  jumps = [2, 3, 1, 1, 4]
Output: True
Why:    jump 1 step to index 1, then 3 steps to the end
Input:  jumps = [3, 2, 1, 0, 4]
Output: False
Why:    every route lands on index 3, whose value 0 goes nowhere
Input:  jumps = [0]
Output: True
Why:    edge case, you already stand on the last index

Hints

0 / 3

Stuck on the idea rather than the code? Jump Game covers it.