medium Jump Game
Problem Statement
You are given an array of integers nums. You start at the first index of the array and each element in nums represents the maximum number of steps you can jump forward from that position.
Return true if you can reach the last index, or false otherwise.
Examples
Example 1
- Input:
nums = [1, 2, 3, 4, 5] - Expected Output:
true - Justification: Starting at index 0, you can jump 1 step to index 1. From index 1, you can jump 2 steps to index 3. From index 3, you can jump 1 steps to the last index (4). Therefore, it is possible to reach the end.
Example 2
- Input:
nums = [2, 0, 2, 0, 1] - Expected Output:
true - Justification: Starting at index 0, you can jump 2 steps to index 2. From index 2, you can jump 2 steps to the last index (4). Therefore, it is possible to reach the end.
Example 3
- Input:
nums = [1, 0, 1, 0] - Expected Output:
false - Justification: Starting at index 0, you can jump 1 step to index 1. However, index 1 has a value of 0, which means you cannot move forward from there. Therefore, it is impossible to reach the end.
Constraints:
- 1 <= nums.length <= 104
- 0 <= nums[i] <= 105
Try it yourself
Try solving this question here:
After you try — Pattern Transfer
Pattern: GREEDY FARTHEST-REACH (Jump Game family).
Template (reachability): maxReach=0; for each i: if i > maxReach return false; maxReach = max(maxReach, i+nums[i]); return true.
When NOT: min number of jumps → same family with BFS layers (Jump II); jump fees / weights → DP or shortest path; not sliding window.
Edges
[0] → true(already at last index).[0,1] → false;[2,0,0] → true.
🎯 STRICT STANDOUT: Why / complexity derivation / pattern+when-not / edges / drills — Jump Game medium
Why this exists (judgment layer)
Reachability on a line with max jump lengths is the poster child for greedy farthest-reach — linear time proof that you never need full BFS/DP for the yes/no form.
Worked example & complexity derivation
Template: maxReach=0
for i in 0..n-1:
if i > maxReach: return false # stranded
maxReach = max(maxReach, i + nums[i])
if maxReach >= n-1: return true
[1,0,1,0]: i=0 reach=1; i=1 ≤1 but 1+0=1; i=2 >1 → false
[2,0,2,0,1]: i=0 reach=2; i=2 reach=max(2,4) ≥ last → true
Time O(n) one pass; space O(1)
Correctness sketch: maxReach is the farthest index reachable from any index ≤ current frontier; if i exceeds it, no prior jump lands on i
Pattern transfer & when-NOT
Pattern: GREEDY FARTHEST-REACH (Jump Game family). When-NOT: min jump count → Jump Game II (BFS layers / greedy windows); weighted jumps → DP/shortest path; graph with obstacles → BFS. Not sliding window.
Edge cases (hand-run)
[0] → true (already at last). [0,1] → false. [2,0,0] → true. All zeros after start with nums[0]=0 and n>1 → false.
Hostile-panel drills (defend the decision)
Q1. Why greedy works for reachability but not always for min jumps without care?
Model answer: Yes/no only needs the reachable set's right frontier; min jumps needs layer expansion (or Jump II greedy on windows) to count hops.
Q2. Hand-run [3,2,1,0,4].
Model answer: maxReach progresses 3, max(3,1+2=3), max(3,2+1=3), max(3,3+0=3); i=4 > 3 → false.
Q3. Space of DP boolean reachable[i] vs greedy?
Model answer: DP O(n) space (or bitset); greedy O(1) by tracking only the frontier.
✅ Solution Jump Game
Problem Statement
You are given an array of integers nums. You start at the first index of the array and each element in nums represents the maximum number of steps you can jump forward from that position.
Return true if you can reach the last index, or false otherwise.
Examples
Example 1
- Input:
nums = [1, 2, 3, 4, 5] - Expected Output:
true - Justification: Starting at index 0, you can jump 1 step to index 1. From index 1, you can jump 2 steps to index 3. From index 3, you can jump 1 steps to the last index (4). Therefore, it is possible to reach the end.
Example 2
- Input:
nums = [2, 0, 2, 0, 1] - Expected Output:
true - Justification: Starting at index 0, you can jump 2 steps to index 2. From index 2, you can jump 2 steps to the last index (4). Therefore, it is possible to reach the end.
Example 3
- Input:
nums = [1, 0, 1, 0] - Expected Output:
false - Justification: Starting at index 0, you can jump 1 step to index 1. However, index 1 has a value of 0, which means you cannot move forward from there. Therefore, it is impossible to reach the end.
Constraints:
- 1 <= nums.length <= 104
- 0 <= nums[i] <= 105
Solution
To solve this problem, we will use a greedy approach. The idea is to keep track of the farthest index that can be reached while iterating through the array. If at any point, the current index exceeds the farthest reachable index, then it's not possible to reach the end, and we return false. If we can reach or surpass the last index, we return true. This approach ensures that we only make necessary jumps and keeps the solution efficient.
This greedy method is effective because it continually updates the farthest reachable index, ensuring that we make the optimal choice at each step. By focusing on the maximum reachability, we avoid unnecessary computations and directly assess the feasibility of reaching the last index.
Step-by-Step Algorithm
- Initialize a variable
maxReachto 0. This will keep track of the farthest index we can reach. - Loop through each index
iof the array nums:- If
iexceedsmaxReach, returnfalse(since we can't move beyondmaxReach). - Update
maxReachto the maximum ofmaxReachandi + nums[i].
- If
- If the loop completes, return
true(since we can reach or exceed the last index).
Algorithm Walkthrough
Let's walk through the algorithm using the input [2, 0, 2, 0, 1]:
- Initialize
maxReachto 0. - At index 0:
nums[0]is 2.- Check if
0 > maxReach (0). This is false. - Update
maxReachtomax(0, 0 + 2) = 2.
- At index 1:
nums[1]is 0.- Check if
1 > maxReach (2). This is false. maxReachremains 2 sincemax(2, 1 + 0) = 2.
- At index 2:
nums[2]is 2.- Check if
2 > maxReach (2). This is false. - Update
maxReachtomax(2, 2 + 2) = 4.
- At index 3:
nums[3]is 0.- Check if
3 > maxReach (4). This is false. maxReachremains 4 sincemax(4, 3 + 0) = 4.
- At index 4:
nums[4]is 1.- Check if
4 > maxReach (4). This is false. - Update
maxReachtomax(4, 4 + 1) = 5.
Since maxReach is greater than or equal to the last index (4), we return true.
Code
public class Solution {
public boolean canJump(int[] nums) {
// Initialize the maximum reachable index
int maxReach = 0;
// Iterate through each index in the array
for (int i = 0; i < nums.length; i++) {
// If the current index is greater than maxReach, return false
if (i > maxReach) {
return false;
}
// Update the maximum reachable index
maxReach = Math.max(maxReach, i + nums[i]);
}
// If we have iterated through the array, return true
return true;
}
public static void main(String[] args) {
Solution sol = new Solution();
// Example 1
System.out.println(sol.canJump(new int[] { 1, 2, 3, 4, 5 })); // true
// Example 2
System.out.println(sol.canJump(new int[] { 2, 0, 2, 0, 1 })); // true
// Example 3
System.out.println(sol.canJump(new int[] { 1, 0, 1, 0 })); // false
}
}
Complexity Analysis
- Time Complexity:
, where nis the length of the array nums. We only traverse the array once. - Space Complexity:
. We only use a single variable maxReachto store the maximum reachable index, which requires constant space.
Pattern Transfer — GREEDY FARTHEST-REACH (Jump I)
Pattern family: greedy farthest-reach for jump games.
Template (boolean reachability):
maxReach = 0
for i in 0..n-1:
if i > maxReach: return false
maxReach = max(maxReach, i + nums[i])
return true
Invariant: after processing indices 0..i−1 (and checking i), maxReach is the farthest index reachable from any position we can stand on. If i ≤ maxReach, we can stand at i.
When NOT:
- Min jumps → Jump II (BFS layers / same farthest idea counting layers).
- Jump with cost/fee → DP.
- General graph with weights → shortest path.
- Boolean DP
can[i]also works but typically more space / easier to write O(n²).
Complexity: one loop of n iterations, O(1) work each → Θ(n) time, O(1) space.
Edge hand-runs
[0]: i=0, 0>0 false, maxReach=max(0,0+0)=0, done → true.[0,1]: i=0 maxR=0; i=1>0 → false.[2,0,0]: maxR grows to 2 → true.[1,0,1,0]: i=0 maxR=1; i=1 maxR=1; i=2>1 → false.
Drill: State the Jump II question in one sentence and name the extra variables (end, far, jumps).
Recognize it: A locally-optimal choice is provably globally optimal → take the best step now (often after sorting).
▶ Visualize this problem (step it, predict each fork)
🤖 Don't fully get this? Learn it with Claude
Stuck on Jump Game? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Jump Game** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Jump Game** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Jump Game**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Jump Game**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.