LeetCode 45: Jump Game II is a Medium array / string problem. This Python walkthrough develops a greedy BFS frontier 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 BFS frontier |
| 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, scan the current jump range while computing the farthest next range. Crossing the current range boundary commits exactly one additional jump. The invariant worth writing beside the code is: The current boundary contains exactly the indices reachable with the current jump count.
Step-by-step algorithm
- Identify the input state consumed by
jump(nums)and initialize the data required by the greedy BFS frontier pattern. - Scan the current jump range while computing the farthest next range. Crossing the current range boundary commits exactly one additional jump.
- After each update, verify the page’s central invariant: The current boundary contains exactly the indices reachable with the current jump count.
- Finish only after the boundary behavior is covered: The input guarantees reachability; a one-element input needs zero jumps.
Python solution
class Solution:
def jump(self, nums):
jumps = end = farthest = 0
for i in range(len(nums) - 1):
farthest = max(farthest, i + nums[i])
if i == end:
jumps += 1
end = farthest
return jumpsReading the implementation
The main entry point is jump(nums). The named working state includes jumps, farthest, end; those variables make the greedy BFS frontier 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. In concrete terms, scan the current jump range while computing the farthest next range. Crossing the current range boundary commits exactly one additional jump.
Correctness argument
Initialization. The data structure starts with exactly the information known before any input element is processed.
Preservation. Scan the current jump range while computing the farthest next range. Crossing the current range boundary commits exactly one additional jump. Each update records the current item without invalidating earlier decisions; consequently, the current boundary contains exactly the indices reachable with the current jump count.
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().jump([2,3,1,1,4]) == 2Common mistakes and edge cases
- Problem-specific boundary: The input guarantees reachability; a one-element input needs zero jumps.
- 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 the current boundary contains exactly the indices reachable with the current jump count.
Interview review checklist
- Explain why greedy BFS frontier matches the structure of this input.
- State the invariant in one sentence before tracing code: The current boundary contains exactly the indices reachable with the current jump count.
- Derive O(n) time and O(1) extra space from how many times each element or state is visited.
- Test the boundary explicitly: The input guarantees reachability; a one-element input needs zero jumps.
Browse the searchable 100 LeetCode Python Solutions hub. Previous: 55. Jump Game · Next: 274. H-Index