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 + 3from 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
Example 3
π 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
jumpsand updateend = farto 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++)
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Two-Pointer Range Update
π‘ Algorithm Steps:
Use two pointers to track current reachable range boundaries.
Expand the far pointer to maximum reachable position in current range.
When current range is exhausted, increment jumps and update range.
Continue until destination is within reachable range.
π Complexity Analysis:
Time: β±οΈ O(n) - Each position visited at most twice
Auxiliary Space: πΎ O(1) - Only constant extra variables
β
Why This Approach?
Explicit range-based processing
Clear separation of reachable segments
Easy to understand the jump boundaries
π 3οΈβ£ Segment-Based Greedy
π‘ Algorithm Steps:
Process array in segments where each segment represents positions reachable in current jumps.
For current segment, find the maximum reachable position for next jump.
Move to next segment when current segment is exhausted.
Count segments traversed as minimum jumps needed.
π Complexity Analysis:
Time: β±οΈ O(n) - Single pass through the array
Auxiliary Space: πΎ O(1) - Only constant extra variables
β
Why This Approach?
Explicit boundary checking for impossible cases
Clear segment-based thinking
Robust handling of edge cases
π 4οΈβ£ BFS-Style Level Processing
π‘ Algorithm Steps:
Treat each jump as a level in BFS traversal.
Process all positions reachable in current level.
Find all positions reachable in next level.
Continue until target position is reached.
π Complexity Analysis:
Time: β±οΈ O(n) - Process each element once
Auxiliary Space: πΎ O(1) - Constant space usage
β
Why This Approach?
BFS-like level-order processing
Clear conceptual model
Natural jump counting mechanism
π π Comparison of Approaches
π Approach
β±οΈ Time Complexity
πΎ Space Complexity
β Pros
β οΈ Cons
π― Greedy Range
π’ O(n)
π’ O(1)
π Optimal performance, clean
π§ Requires greedy insight
π Two-Pointer Range
π’ O(n)
π’ O(1)
β Explicit range handling
π§ More complex loop structure
π Segment Greedy
π’ O(n)
π’ O(1)
β Robust edge case handling
π§ More verbose implementation
π BFS-Style Level
π’ O(n)
π’ O(1)
π§ Intuitive BFS-like thinking
π Similar to greedy but less direct
π Best Choice Recommendation
π― Scenario
ποΈ Recommended Approach
π₯ Performance Rating
π Competitive Programming
π₯ Greedy Range
β β β β β
π― Interview/Clean Code
π₯ Segment Greedy
β β β β β
π§ Learning/Understanding
π₯ BFS-Style Level
β β β β β
π§ Explicit Range Control
ποΈ Two-Pointer Range
β β β β β
β Code (Java)
π Code (Python)
π§ 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