-
-
Notifications
You must be signed in to change notification settings - Fork 155
minimum number od jumps solution added #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
356 changes: 356 additions & 0 deletions
356
dsa-solutions/gfg-solutions/0022-minimum-number-of-jumps.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,356 @@ | ||
--- | ||
id: minimum number of jumps | ||
title: Minimum number of jumps | ||
sidebar_label: 0022 Minimum number of jumps | ||
tags: | ||
- Arrays | ||
- greedy | ||
- Data Structure | ||
- Algorithms | ||
description: "find the minimum number of jumps required to reach the end of an array, given that each element represents the maximum length of the jump that can be made from that position." | ||
|
||
--- | ||
## Problem Description | ||
|
||
The problem is to find the minimum number of jumps required to reach the end of an array. Each element in the array represents the maximum length of the jump that can be made from that element. If an element is 0, it means no movement can be made through that element. The task is to return the minimum number of jumps to reach the end of the array starting from the first element. If reaching the end is not possible, the function should return -1. | ||
|
||
## Example | ||
|
||
Consider an array of 9 elements starting from 1 to 9 . | ||
The minimum number of jumps required would be 3: | ||
1. Jump from the 1st element to the 2nd element (value 3). | ||
2. From the 2nd element, jump to the 5th element (value 9). | ||
3. Finally, jump to the last element. | ||
|
||
## Expected Complexity | ||
|
||
- Time Complexity: $(O(N))$ | ||
- Space Complexity: $(O(1))$ | ||
|
||
## Solutions | ||
|
||
### Brute Force Approach | ||
|
||
#### Intuition | ||
|
||
The brute force approach involves recursively checking all possible jumps from each element, calculating the minimum number of jumps needed to reach the end from reachable elements. | ||
|
||
#### Implementation | ||
|
||
The implementation recursively checks all possible jumps from each element, updating the minimum number of jumps needed. | ||
|
||
```cpp | ||
// C++ Implementation | ||
Yashgabani845 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Solution { | ||
public: | ||
int minimumJumps(int arr[], int n) { | ||
if (n == 1) | ||
return 0; | ||
|
||
int ans = INT_MAX; | ||
for (int i = n - 2; i >= 0; i--) { | ||
if (i + arr[i] >= n - 1) { | ||
int sub_ans = minimumJumps(arr, i + 1); | ||
if (sub_ans != INT_MAX) | ||
ans = min(ans, sub_ans + 1); | ||
} | ||
} | ||
return (ans==INT_MAX ? -1 : ans); | ||
} | ||
}; | ||
``` | ||
|
||
### Brute Optimized Approach | ||
|
||
#### Intuition | ||
|
||
In this approach, we use dynamic programming to store the minimum number of jumps required to reach each index from the start. | ||
|
||
#### Implementation | ||
|
||
The code initializes an array to store the minimum jumps required and iterates through the array to calculate the minimum jumps. | ||
|
||
```cpp | ||
// C++ Implementation | ||
Yashgabani845 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Solution { | ||
public: | ||
int minimumJumps(int arr[], int n) { | ||
int jumps[n]; | ||
if (arr[0] == 0) | ||
return -1; | ||
|
||
jumps[0] = 0; | ||
for (int i = 1; i < n; i++) { | ||
jumps[i] = INT_MAX; | ||
for (int j = 0; j < i; j++) { | ||
if (i <= j + arr[j] && jumps[j] != INT_MAX) { | ||
jumps[i] = min(jumps[i], jumps[j] + 1); | ||
break; | ||
} | ||
} | ||
} | ||
return (jumps[n - 1] == INT_MAX ? -1 : jumps[n-1]); | ||
} | ||
}; | ||
``` | ||
|
||
#### Python Implementation | ||
|
||
```python | ||
# Python Implementation | ||
Yashgabani845 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Solution: | ||
def minJumps(self, arr, n): | ||
if len(arr) <= 1 : | ||
return 0 | ||
|
||
# Return -1 if not possible to jump | ||
if arr[0] == 0 : | ||
return -1 | ||
|
||
# initialization | ||
maxReach = arr[0]; | ||
step = arr[0]; | ||
jump = 1; | ||
|
||
|
||
# Start traversing array | ||
for i in range(1,len(arr)): | ||
|
||
# Check if we have reached the end of the array | ||
if i == len(arr) - 1 : | ||
return jump | ||
|
||
# updating maxReach | ||
maxReach = max(maxReach, i+arr[i]) | ||
|
||
# we use a step to get to the current index | ||
step-=1; | ||
|
||
# If no further steps left | ||
if step == 0 : | ||
# we must have used a jump | ||
jump+=1 | ||
|
||
#Check if the current index/position or lesser index | ||
#is the maximum reach point from the previous indexes | ||
if i>=maxReach : | ||
return -1 | ||
|
||
#re-initialize the steps to the amount | ||
#of steps to reach maxReach from position i. | ||
step = maxReach - i | ||
|
||
return -1 | ||
``` | ||
|
||
#### Java Implementation | ||
|
||
```java | ||
// Java Implementation | ||
class Solution { | ||
static int minJumps(int arr[]) | ||
{ | ||
if (arr.length <= 1) | ||
return 0; | ||
|
||
// Return -1 if not possible to jump | ||
if (arr[0] == 0) | ||
return -1; | ||
|
||
// initialization | ||
int maxReach = arr[0]; | ||
int step = arr[0]; | ||
int jump = 1; | ||
|
||
|
||
// Start traversing array | ||
for (int i = 1; i < arr.length; i++) | ||
{ | ||
// Check if we have reached the end of the array | ||
if (i == arr.length - 1) | ||
return jump; | ||
|
||
// updating maxReach | ||
maxReach = Math.max(maxReach, i+arr[i]); | ||
|
||
// we use a step to get to the current index | ||
step--; | ||
|
||
// If no further steps left | ||
if (step == 0) | ||
{ | ||
// we must have used a jump | ||
jump++; | ||
|
||
//Check if the current index/position or lesser index | ||
// is the maximum reach point from the previous indexes | ||
if(i>=maxReach) | ||
return -1; | ||
|
||
// re-initialize the steps to the amount | ||
// of steps to reach maxReach from position i. | ||
step = maxReach - i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
``` | ||
|
||
### Expected Approach | ||
|
||
#### Intuition | ||
|
||
This approach involves maintaining the maximum reachable index from the current index and the number of steps we can jump if we jump from the current index. By iterating through the array, we update these values accordingly. | ||
|
||
#### Implementation | ||
|
||
The implementation traverses the array, updating the maximum reachable index and the number of steps at each iteration. | ||
|
||
```cpp | ||
// C++ Implementation | ||
class Solution { | ||
public: | ||
int minJumps(int arr[], int n) { | ||
if (n <= 1) | ||
return 0; | ||
if (arr[0] == 0) | ||
return -1; | ||
|
||
int maxReach = arr[0]; | ||
int step = arr[0]; | ||
int jump = 1; | ||
|
||
for (int i = 1; i < n; i++) { | ||
if (i == n - 1) | ||
return jump; | ||
maxReach = max(maxReach, i+arr[i]); | ||
step--; | ||
if (step ==0) { | ||
jump++; | ||
if (i >= maxReach) | ||
return -1; | ||
step = maxReach - i; | ||
} | ||
} | ||
return -1; | ||
} | ||
}; | ||
``` | ||
|
||
#### Python Implementation | ||
|
||
```python | ||
# Python Implementation | ||
class Solution: | ||
def minJumps(self, arr, n): | ||
if len(arr) <= 1 : | ||
return 0 | ||
|
||
# Return -1 if not possible to jump | ||
if arr[0] == 0 : | ||
return -1 | ||
|
||
# initialization | ||
maxReach = arr[0]; | ||
step = arr[0]; | ||
jump = 1; | ||
|
||
|
||
# Start traversing array | ||
for i in range(1,len(arr)): | ||
|
||
# Check if we have reached the end of the array | ||
if i == len(arr) - 1 : | ||
return jump | ||
|
||
# updating maxReach | ||
maxReach = max(maxReach, i+arr[i]) | ||
|
||
# we use a step to get to the current index | ||
step-=1; | ||
|
||
# If no further steps left | ||
if step == 0 : | ||
# we must have used a jump | ||
jump+=1 | ||
|
||
#Check if the current index/position or lesser index | ||
#is the maximum reach point from the previous indexes | ||
if i>=maxReach : | ||
return -1 | ||
|
||
#re-initialize the steps to the amount | ||
#of steps to reach maxReach from position i. | ||
step = maxReach - i | ||
|
||
return -1 | ||
``` | ||
|
||
#### Java Implementation | ||
|
||
```java | ||
// Java Implementation | ||
class Solution { | ||
static int minJumps(int arr[]) | ||
{ | ||
if (arr.length <= 1) | ||
return 0; | ||
|
||
// Return -1 if not possible to jump | ||
if (arr[0] == 0) | ||
return -1; | ||
|
||
// initialization | ||
int maxReach = arr[0]; | ||
int step = arr[0]; | ||
int jump = 1; | ||
|
||
|
||
// Start traversing array | ||
for (int i = 1; i < arr.length; i++) | ||
{ | ||
// Check if we have reached the end of the array | ||
if (i == arr.length - 1) | ||
return jump; | ||
|
||
// updating maxReach | ||
maxReach = Math.max(maxReach, i+arr[i]); | ||
|
||
// we use a step to get to the current index | ||
step--; | ||
|
||
// If no further steps left | ||
if (step == 0) | ||
{ | ||
// we must have used a jump | ||
jump++; | ||
|
||
//Check if the current index/position or lesser index | ||
// is the maximum reach point from the previous indexes | ||
if(i>=maxReach) | ||
return -1; | ||
|
||
// re-initialize the steps to the amount | ||
// of steps to reach maxReach from position i. | ||
step = maxReach - i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
``` | ||
|
||
## Conclusion | ||
|
||
In conclusion, the problem of finding the minimum number of jumps to reach the end of an array can be efficiently solved using dynamic programming or an optimized approach that tracks the maximum reachable index and the number of steps available. The expected approach offers the best time complexity of $(O(N))$ and space complexity of $(O(1))$, making it the most efficient solution for large input sizes. | ||
|
||
By implementing these solutions, we can effectively solve the problem with minimal time and space complexity, providing a scalable solution for real-world applications. | ||
|
||
## References | ||
|
||
- **GeeksforGeeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1) | ||
- **Solution Link:** [Minimum Number of jumps on Geeks for Geeks](https://www.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1) | ||
- **Authors GeeksforGeeks Profile:** [Yash](https://www.geeksforgeeks.org/user/gabaniyxn26/) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.