medium +20 pts

Jump Game Reachable

Determine if you can reach the last index given maximum jump lengths.

You are given a list `nums` of non-negative integers. You start at the first index (index 0). At each index `i`, you can jump to any index in the range `[i, i + nums[i]]` (inclusive). Return `True` if you can reach the last index, otherwise return `False`. Implement the function `can_reach(nums)` that takes a list of non-negative integers and returns a boolean. **Examples:** - `can_reach([2,3,1,1,4])` returns `True` because you can jump 1 step to index 1, then 3 steps to the last index. - `can_reach([3,2,1,0,4])` returns `False` because you get stuck at index 3 with 0 jump length. - `can_reach([0])` returns `True` because you are already at the last index. Constraints: - `1 <= len(nums) <= 10^5` - `0 <= nums[i] <= 10^5` Your solution should run in O(n) time and O(1) space.

Constraints

1 ≤ len(nums) ≤ 100,000; 0 ≤ nums[i] ≤ 100,000. Time O(n), space O(1).

Example

>>> can_reach([2,3,1,1,4])
True
>>> can_reach([3,2,1,0,4])
False
>>> can_reach([0])
True
20 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Track the furthest index you can currently reach as you scan from left to right.
If the current index is ever greater than the furthest reachable, you are stuck and can return False.
Update the furthest reachable as max(current furthest, i + nums[i]).
If the furthest reachable reaches or exceeds the last index, stop and return True.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.