Opening your learning path
Progress is saved as you study.
Opening your learning path
Progress is saved as you study.
The Jump Game Reachability/Minimization pattern applies when you need the Jump Game Reachability/Minimization technique within the Greedy Patterns pattern. Its time complexity is O(n) and space complexity O(1). It is used in 2 problems on Thita, including Jump Game and Jump Game II. Common variations are Sorting Based and Buy/Sell Stock.
Solve jump game problems using greedy approach. Check reachability and find minimum jumps.
Jump Game Reachability/Minimization is one of the 7 subpatterns of the Greedy Patterns pattern, which covers interval scheduling, jump games, stock trading, and task scheduling. The whole pattern is about 3 hours of study. This subpattern is a core technique: expect it to come up directly in interviews.
Master greedy algorithms including interval scheduling, jump games, stock trading, and task scheduling. Make locally optimal choices for global optimization. Problems in this subpattern are usually searched for as jump game, can reach, minimum jumps, greedy jump, leetcode 55, leetcode 45.
Read the theory for Jump Game Reachability/Minimization, then work the problems attached to it in the browser editor. Your solution runs against the problem's test cases, and the AI coach offers a hint about the technique you are missing rather than a finished solution. Progress is tracked per subpattern, so the Greedy Patterns tracker shows this one as covered once you have solved its problems.
Jump Game Reachability/Minimization is one lesson in a 16-pattern DSA course. If you are preparing end to end, work the interview-critical patterns first and use the pattern sheet as the checklist; if you are here for one technique, the Greedy Patterns guide is the shortest path back to the rest of it.
2 problems, easiest first.
Imagine you’re crossing a series of stepping stones over a river. Each stone tells you the maximum number of stones you can jump ahead from there. Your goal is to reach the other side—sometimes just to see if it’s possible, other times to do it in as few jumps as possible. This is the heart of the Jump Game Reachability/Minimization subpattern.
This pattern is all about making the smartest local choices (greedy decisions) to achieve a global goal, like reaching the end of an array or minimizing the number of moves. Think of it like playing hopscotch: at every square, you want to leap as far as possible without overshooting, making sure you don’t get stuck on a square with nowhere to go.
You’ll see this pattern in scenarios where you must traverse a path with variable step sizes, such as:
In coding interviews, especially with companies like Amazon, Apple, and Facebook, recognizing when a problem is about reachability or minimizing steps in an array is a key skill. The greedy approach shines here because it often leads to efficient, elegant solutions.
Let’s break down the main ideas behind this subpattern:
Let’s build a mental model for solving Jump Game problems:
farthest: the farthest index you can reach so far.currentEnd: the end of the current jump range.jumps: (for minimization) how many jumps you’ve made.farthest with the maximum of its current value and i + nums[i].i == currentEnd), you must jump:
jumps.currentEnd to farthest.i exceeds farthest, you’re stuck—return false (for reachability).Suppose nums = [2,3,1,1,4]:
Let’s look at two code examples: one for reachability (Jump Game) and one for minimization (Jump Game II).
1// Problem: Jump Game
2// Goal: Return true if you can reach the last index
3
4#include <vector>
5using namespace std;
6
7class Solution {
8public:
9 bool canJump(vector<int>& nums) {
10 int farthest = 0; // The farthest index we can reach so far
11 int n = nums.size();
12 // Loop through each index
13 for (int i = 0; i < n; ++i) {
14 // If we are at an index beyond our reach, we can't proceed
15 if (i > farthest) return false;
16 // Update the farthest reachable index from here
17 farthest = max(farthest, i + nums[i]);
18 // If we can reach or pass the last index, return true
19 if (farthest >= n - 1) return true;
20 }
21 // If we finish the loop, check if we reached the end
22 return false;
23 }
24};Explanation:
farthest keeps track of the maximum index you can reach at any step.i > farthest), you return false.1// Problem: Jump Game II
2// Goal: Return the minimum number of jumps to reach the last index
3
4#include <vector>
5using namespace std;
6
7class Solution {
8public:
9 int jump(vector<int>& nums) {
10 int n = nums.size();
11 int jumps = 0; // Number of jumps made
12 int currentEnd = 0; // End of the current jump range
13 int farthest = 0; // Farthest index reachable in the current jump
14
15 // We don't need to jump from the last element
16 for (int i = 0; i < n - 1; ++i) {
17 // Update the farthest we can reach from here
18 farthest = max(farthest, i + nums[i]);
19 // If we've reached the end of the current jump, we need to jump
20 if (i == currentEnd) {
21 jumps++;
22 currentEnd = farthest; // Set the new jump range
23 }
24 }
25 return jumps;
26 }
27};Explanation:
jumps counts how many jumps you’ve made.currentEnd marks the end of your current jump’s reach.currentEnd, you must jump again, so you increment jumps and set currentEnd to the new farthest.Let’s analyze Jump Game II step by step:
Problem Recap:
Given an array nums, each element tells you the maximum jump length from that position. Find the minimum number of jumps needed to reach the last index.
Greedy Jump Game II: walk the array, keep farthest, and when i reaches currentEnd take a jump and set currentEnd = farthest. Two zones cover indices 1 to 4, so the answer is 2 jumps.
Step-by-Step Intuition:
Visualize the Problem:
Imagine you’re standing at the start of a path. Each spot tells you how far you can leap. You want to reach the end in as few jumps as possible.
Key Insight:
At each position, you want to maximize how far you can get with each jump. This is like moving through “zones”—each jump takes you as far as possible within the current zone.
How the Greedy Approach Works:
farthest).currentEnd), you must jump to continue.Why This Works:
By always tracking the farthest reachable spot, you minimize the number of jumps. You never make unnecessary short jumps.
Example Walkthrough:
For nums = [2,3,1,1,4]:
jumps = 0, currentEnd = 0, farthest = 0).farthest = max(0, 0+2) = 2.i reaches currentEnd (0), you jump (jumps = 1, currentEnd = 2).farthest = max(2, 1+3) = 4.farthest = max(4, 2+1) = 4.i reaches currentEnd (2), you jump again (jumps = 2, currentEnd = 4).Connection to the Pattern:
This approach is greedy because at every step, you look for the farthest you can go, ensuring you never waste a jump.
Signals to Recognize This Pattern:
Common “Aha!” Moments:
Debugging Tips:
Memory Tricks:
With practice, you’ll spot Jump Game patterns quickly and solve them with confidence!