11. Minimum Jumps
β GFG solution to the Minimum Jumps problem: find minimum number of jumps to reach the end of array using greedy range-based approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[]
of non-negative numbers. Each number tells you the maximum number of steps you can jump forward from that position.
For example:
If
arr[i] = 3
, you can jump to indexi + 1
,i + 2
, ori + 3
from positioni
.If
arr[i] = 0
, you cannot jump forward from that position.
Your task is to find the minimum number of jumps needed to move from the first position in the array to the last position.
Note: Return -1
if you can't reach the end of the array.
π Examples
Example 1
Input: arr[] = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
Output: 3
Explanation: First jump from 1st element to 2nd element with value 3.
From here we jump to 5th element with value 9, and from here we will jump to the last.
Example 2
Input: arr[] = [1, 4, 3, 2, 6, 7]
Output: 2
Explanation: First we jump from the 1st to 2nd element and then jump to the last element.
Example 3
Input: arr[] = [0, 10, 20]
Output: -1
Explanation: We cannot go anywhere from the 1st element.
π Constraints
$2 \le \text{arr.size()} \le 10^5$
$0 \le \text{arr}[i] \le 10^5$
β
My Approach
The optimal approach uses a Greedy Range-Based Strategy that processes the array in segments, where each segment represents positions reachable within the current number of jumps:
Greedy Range-Based Algorithm
Initialize Variables:
jumps
= 0 (number of jumps taken)far
= 0 (farthest position reachable with current jumps)end
= 0 (end of current reachable range)
Process Each Position:
For each position
i
, updatefar = max(far, i + arr[i])
to find the farthest reachable position.When we reach the end of current range (
i == end
), we must make a jump.Increment
jumps
and updateend = far
to extend our reachable range.
Early Termination:
If
end >= n - 1
, we can reach the last position, returnjumps
.If we exhaust all positions without reaching the end, return
-1
.
Key Insight:
We use a greedy strategy: within each reachable range, we find the position that allows us to jump the farthest in the next jump.
This ensures we make the minimum number of jumps.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We traverse the array once, and each element is processed exactly once.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables (jumps, far, end).
π§βπ» Code (C++)
class Solution {
public:
int minJumps(vector<int>& arr) {
int n = arr.size(), jumps = 0, far = 0, end = 0;
for (int i = 0; i < n - 1; i++) {
far = max(far, i + arr[i]);
if (i == end) jumps++, end = far;
if (end >= n - 1) return jumps;
}
return -1;
}
};
β Code (Java)
class Solution {
public int minJumps(int[] arr) {
int n = arr.length, jumps = 0, far = 0, end = 0;
for (int i = 0; i < n - 1; i++) {
far = Math.max(far, i + arr[i]);
if (i == end) { jumps++; end = far; }
if (end >= n - 1) return jumps;
}
return -1;
}
}
π Code (Python)
class Solution:
def minJumps(self, arr):
n, jumps, far, end = len(arr), 0, 0, 0
for i in range(n - 1):
far = max(far, i + arr[i])
if i == end:
jumps += 1
end = far
if end >= n - 1:
return jumps
return -1
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated