LeetCode 55: Jump Game is a Medium array / string problem. This Python walkthrough develops a greedy reachability solution, ties every code decision to a concrete invariant, and includes the regression check used before publication.
This independent guide paraphrases the task rather than reproducing LeetCode’s prompt. Use the official page for the exact statement, examples, constraints, and submission runner.
| Difficulty | Medium |
|---|---|
| Topic | Array / String |
| Reusable pattern | greedy reachability |
| Complexity | O(n) time and O(1) extra space |
Recognizing the pattern
Array and string questions usually reward a precise index invariant. Decide which prefix or suffix is already final before mutating the next position.
For this problem specifically, maintain the farthest reachable index. If the scan reaches an index beyond that frontier, progress is impossible; otherwise extend the frontier. The invariant worth writing beside the code is: Every index at or before farthest is reachable from the start.
Step-by-step algorithm
- Identify the input state consumed by
canJump(nums)and initialize the data required by the greedy reachability pattern. - Maintain the farthest reachable index. If the scan reaches an index beyond that frontier, progress is impossible; otherwise extend the frontier.
- After each update, verify the page’s central invariant: Every index at or before farthest is reachable from the start.
- Finish only after the boundary behavior is covered: A one-element array succeeds; zeros matter only when they stop the frontier.
Python solution
class Solution:
def canJump(self, nums):
farthest = 0
for i, jump in enumerate(nums):
if i > farthest:
return False
farthest = max(farthest, i + jump)
return TrueReading the implementation
The main entry point is canJump(nums). The named working state includes farthest; those variables make the greedy reachability state visible instead of hiding it in incidental control flow.
A single main loop advances the algorithm, which is the key reason the traversal does not revisit already resolved input unnecessarily. Early returns stop as soon as the answer is forced, avoiding work that cannot change the result. In concrete terms, maintain the farthest reachable index. If the scan reaches an index beyond that frontier, progress is impossible; otherwise extend the frontier.
Correctness argument
Initialization. The data structure starts with exactly the information known before any input element is processed.
Preservation. Maintain the farthest reachable index. If the scan reaches an index beyond that frontier, progress is impossible; otherwise extend the frontier. Each update records the current item without invalidating earlier decisions; consequently, every index at or before farthest is reachable from the start.
Termination. The traversal consumes a finite input or finite state space. At the end, the invariant covers the complete input, which is precisely the condition required for the returned result.
Complexity and trade-offs
O(n) time and O(1) extra space. The auxiliary-space figure excludes the returned output unless the output itself is the structure being built.
A copied output buffer can simplify reasoning, but the in-place version reduces auxiliary memory when mutation is allowed. That comparison is useful in an interview because it explains why the final implementation is preferable, not merely that it passes.
Regression check
The published implementation belongs to a 100-problem suite that is compiled and exercised behaviorally before deployment.
One reference assertion from that suite is shown below. It targets the normal path while the edge conditions in the next section cover the failure-prone boundaries.
assert Solution().canJump([2,3,1,1,4]) and not Solution().canJump([3,2,1,0,4])Common mistakes and edge cases
- Problem-specific boundary: A one-element array succeeds; zeros matter only when they stop the frontier.
- Pattern-level pitfall: Do not let a write operation destroy input that a later read still needs; write direction and boundary conventions matter.
- Invariant check: after every update, confirm that every index at or before farthest is reachable from the start.
Interview review checklist
- Explain why greedy reachability matches the structure of this input.
- State the invariant in one sentence before tracing code: Every index at or before farthest is reachable from the start.
- Derive O(n) time and O(1) extra space from how many times each element or state is visited.
- Test the boundary explicitly: A one-element array succeeds; zeros matter only when they stop the frontier.
Browse the searchable 100 LeetCode Python Solutions hub. Previous: 122. Best Time to Buy and Sell Stock II · Next: 45. Jump Game II