Why it works
- The cells up to end are exactly those reachable in jumps jumps, and the cells after end up to far need one more. The run walks over the array range by range, like a breadth-first search, but without a queue.
- Which cell the next jump starts from is decided only when i reaches end: by then every cell of the range has been seen, and far is the farthest of their jumps. Nothing is chosen before all the options are known.
- The loop stops before the last index: landing on it ends the trip, and counting a jump from there would add one too many.
Complexity
Time O(n): one pass over the array. Memory O(1): only jumps, end and far.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗