Skip to content

Commit fa84632

Browse files
committed
minimum number od jumps solution added
1 parent 4db6dc1 commit fa84632

File tree

1 file changed

+356
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)