Jump Game – Solution & Complexity
1. Understand Reachability
- Each value tells you the maximum jump length from that index.
- You only need to know whether the last index can be reached, not the exact path.
- If you ever stand beyond the farthest reachable index, progress is impossible.
2. Brute Force or DP Idea
- One approach marks which indices are reachable from earlier indices.
- For every reachable index, mark all destinations within its jump range.
- This can work, but it may take O(n²) time in large arrays.
3. Track the Farthest Reach
- A greedy scan is enough because jumps only move forward.
- Maintain
farthest, the greatest index reachable so far. - At each reachable index, update
farthestwithi + nums[i].
4. Detect Failure or Success
- If the current index is greater than
farthest, there is no way to reach it, so returnFalse. - If
farthestreaches or passes the last index, returnTrue. - Otherwise continue scanning reachable positions.
5. Final Complete Solution
- The greedy invariant is that all indices up to
farthestare reachable while scanning. - Time complexity is O(n) because each index is visited once.
- Space complexity is O(1).