Jump Game
MediumNeetCode 150Top Interview 150 GreedyNikeNielsenHashedInNaviProblem
You stand on index 0 of an array nums. The number nums[i] is the longest
jump you can make from index i; you may also jump any shorter distance, including 1.
Return true if you can reach the last index, otherwise false.
Constraints
- 1 ≤ nums.length ≤ 10000
- 0 ≤ nums[i] ≤ 100000
- i + nums[i] stays far below 2^31, so int is safe
Examples: pick one to animate
Solutions
TimeO(n)
SpaceO(1)
Time: One pass over the array, with constant work per index.
Space: Only the reach and the loop index are stored.
Keep the farthest index you can reach so far. Walk left to right: if the current index is beyond that reach, you are stuck and the answer is false. Otherwise push the reach out to i + nums[i] if it is larger. Finishing the loop means the last index is reachable.