Skip to content

Commit ee4c936

Browse files
authored
Merge pull request #565 from Yashgabani845/y1
minimum number od jumps solution added
2 parents 43a28f4 + 2245fb7 commit ee4c936

File tree

1 file changed

+349
-0
lines changed

1 file changed

+349
-0
lines changed
Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
1+
---
2+
id: minimum-number-of-jumps
3+
title: Minimum number of jumps
4+
sidebar_label: 0022 Minimum number of jumps
5+
tags:
6+
- Arrays
7+
- greedy
8+
- Data Structure
9+
- Algorithms
10+
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."
11+
12+
---
13+
## Problem Description
14+
15+
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.
16+
17+
## Example
18+
19+
Consider an array of 9 elements starting from 1 to 9 .
20+
The minimum number of jumps required would be 3:
21+
1. Jump from the 1st element to the 2nd element (value 3).
22+
2. From the 2nd element, jump to the 5th element (value 9).
23+
3. Finally, jump to the last element.
24+
25+
## Expected Complexity
26+
27+
- Time Complexity: $(O(N))$
28+
- Space Complexity: $(O(1))$
29+
30+
## Solutions
31+
32+
### Brute Force Approach
33+
34+
#### Intuition
35+
36+
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.
37+
38+
#### Implementation
39+
40+
The implementation recursively checks all possible jumps from each element, updating the minimum number of jumps needed.
41+
42+
```cpp
43+
class Solution {
44+
public:
45+
int minimumJumps(int arr[], int n) {
46+
if (n == 1)
47+
return 0;
48+
49+
int ans = INT_MAX;
50+
for (int i = n - 2; i >= 0; i--) {
51+
if (i + arr[i] >= n - 1) {
52+
int sub_ans = minimumJumps(arr, i + 1);
53+
if (sub_ans != INT_MAX)
54+
ans = min(ans, sub_ans + 1);
55+
}
56+
}
57+
return (ans==INT_MAX ? -1 : ans);
58+
}
59+
};
60+
```
61+
62+
### Brute Optimized Approach
63+
64+
#### Intuition
65+
66+
In this approach, we use dynamic programming to store the minimum number of jumps required to reach each index from the start.
67+
68+
#### Implementation
69+
70+
The code initializes an array to store the minimum jumps required and iterates through the array to calculate the minimum jumps.
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int minimumJumps(int arr[], int n) {
76+
int jumps[n];
77+
if (arr[0] == 0)
78+
return -1;
79+
80+
jumps[0] = 0;
81+
for (int i = 1; i < n; i++) {
82+
jumps[i] = INT_MAX;
83+
for (int j = 0; j < i; j++) {
84+
if (i <= j + arr[j] && jumps[j] != INT_MAX) {
85+
jumps[i] = min(jumps[i], jumps[j] + 1);
86+
break;
87+
}
88+
}
89+
}
90+
return (jumps[n - 1] == INT_MAX ? -1 : jumps[n-1]);
91+
}
92+
};
93+
```
94+
95+
#### Python Implementation
96+
97+
```python
98+
class Solution:
99+
def minJumps(self, arr, n):
100+
if len(arr) <= 1 :
101+
return 0
102+
103+
# Return -1 if not possible to jump
104+
if arr[0] == 0 :
105+
return -1
106+
107+
# initialization
108+
maxReach = arr[0];
109+
step = arr[0];
110+
jump = 1;
111+
112+
113+
# Start traversing array
114+
for i in range(1,len(arr)):
115+
116+
# Check if we have reached the end of the array
117+
if i == len(arr) - 1 :
118+
return jump
119+
120+
# updating maxReach
121+
maxReach = max(maxReach, i+arr[i])
122+
123+
# we use a step to get to the current index
124+
step-=1;
125+
126+
# If no further steps left
127+
if step == 0 :
128+
# we must have used a jump
129+
jump+=1
130+
131+
#Check if the current index/position or lesser index
132+
#is the maximum reach point from the previous indexes
133+
if i>=maxReach :
134+
return -1
135+
136+
#re-initialize the steps to the amount
137+
#of steps to reach maxReach from position i.
138+
step = maxReach - i
139+
140+
return -1
141+
```
142+
143+
#### Java Implementation
144+
145+
```java
146+
class Solution {
147+
static int minJumps(int arr[])
148+
{
149+
if (arr.length <= 1)
150+
return 0;
151+
152+
// Return -1 if not possible to jump
153+
if (arr[0] == 0)
154+
return -1;
155+
156+
// initialization
157+
int maxReach = arr[0];
158+
int step = arr[0];
159+
int jump = 1;
160+
161+
162+
// Start traversing array
163+
for (int i = 1; i < arr.length; i++)
164+
{
165+
// Check if we have reached the end of the array
166+
if (i == arr.length - 1)
167+
return jump;
168+
169+
// updating maxReach
170+
maxReach = Math.max(maxReach, i+arr[i]);
171+
172+
// we use a step to get to the current index
173+
step--;
174+
175+
// If no further steps left
176+
if (step == 0)
177+
{
178+
// we must have used a jump
179+
jump++;
180+
181+
//Check if the current index/position or lesser index
182+
// is the maximum reach point from the previous indexes
183+
if(i>=maxReach)
184+
return -1;
185+
186+
// re-initialize the steps to the amount
187+
// of steps to reach maxReach from position i.
188+
step = maxReach - i;
189+
}
190+
}
191+
192+
return -1;
193+
}
194+
}
195+
```
196+
197+
### Expected Approach
198+
199+
#### Intuition
200+
201+
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.
202+
203+
#### Implementation
204+
205+
The implementation traverses the array, updating the maximum reachable index and the number of steps at each iteration.
206+
207+
```cpp
208+
class Solution {
209+
public:
210+
int minJumps(int arr[], int n) {
211+
if (n <= 1)
212+
return 0;
213+
if (arr[0] == 0)
214+
return -1;
215+
216+
int maxReach = arr[0];
217+
int step = arr[0];
218+
int jump = 1;
219+
220+
for (int i = 1; i < n; i++) {
221+
if (i == n - 1)
222+
return jump;
223+
maxReach = max(maxReach, i+arr[i]);
224+
step--;
225+
if (step ==0) {
226+
jump++;
227+
if (i >= maxReach)
228+
return -1;
229+
step = maxReach - i;
230+
}
231+
}
232+
return -1;
233+
}
234+
};
235+
```
236+
237+
#### Python Implementation
238+
239+
```python
240+
class Solution:
241+
def minJumps(self, arr, n):
242+
if len(arr) <= 1 :
243+
return 0
244+
245+
# Return -1 if not possible to jump
246+
if arr[0] == 0 :
247+
return -1
248+
249+
# initialization
250+
maxReach = arr[0];
251+
step = arr[0];
252+
jump = 1;
253+
254+
255+
# Start traversing array
256+
for i in range(1,len(arr)):
257+
258+
# Check if we have reached the end of the array
259+
if i == len(arr) - 1 :
260+
return jump
261+
262+
# updating maxReach
263+
maxReach = max(maxReach, i+arr[i])
264+
265+
# we use a step to get to the current index
266+
step-=1;
267+
268+
# If no further steps left
269+
if step == 0 :
270+
# we must have used a jump
271+
jump+=1
272+
273+
#Check if the current index/position or lesser index
274+
#is the maximum reach point from the previous indexes
275+
if i>=maxReach :
276+
return -1
277+
278+
#re-initialize the steps to the amount
279+
#of steps to reach maxReach from position i.
280+
step = maxReach - i
281+
282+
return -1
283+
```
284+
285+
#### Java Implementation
286+
287+
```java
288+
class Solution {
289+
static int minJumps(int arr[])
290+
{
291+
if (arr.length <= 1)
292+
return 0;
293+
294+
// Return -1 if not possible to jump
295+
if (arr[0] == 0)
296+
return -1;
297+
298+
// initialization
299+
int maxReach = arr[0];
300+
int step = arr[0];
301+
int jump = 1;
302+
303+
304+
// Start traversing array
305+
for (int i = 1; i < arr.length; i++)
306+
{
307+
// Check if we have reached the end of the array
308+
if (i == arr.length - 1)
309+
return jump;
310+
311+
// updating maxReach
312+
maxReach = Math.max(maxReach, i+arr[i]);
313+
314+
// we use a step to get to the current index
315+
step--;
316+
317+
// If no further steps left
318+
if (step == 0)
319+
{
320+
// we must have used a jump
321+
jump++;
322+
323+
//Check if the current index/position or lesser index
324+
// is the maximum reach point from the previous indexes
325+
if(i>=maxReach)
326+
return -1;
327+
328+
// re-initialize the steps to the amount
329+
// of steps to reach maxReach from position i.
330+
step = maxReach - i;
331+
}
332+
}
333+
334+
return -1;
335+
}
336+
}
337+
```
338+
339+
## Conclusion
340+
341+
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.
342+
343+
By implementing these solutions, we can effectively solve the problem with minimal time and space complexity, providing a scalable solution for real-world applications.
344+
345+
## References
346+
347+
- **GeeksforGeeks Problem:** [Geeks for Geeks Problem](https://www.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1)
348+
- **Solution Link:** [Minimum Number of jumps on Geeks for Geeks](https://www.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1)
349+
- **Authors GeeksforGeeks Profile:** [Yash](https://www.geeksforgeeks.org/user/gabaniyxn26/)

0 commit comments

Comments
 (0)