-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinJumps.cpp
35 lines (26 loc) · 926 Bytes
/
MinJumps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<bits/stdc++.h>
using namespace std;
int minJumps(vector<int>& arr) {
int i = 0, jumps = 0;
int n = arr.size();
// * If the first element is 0, we cannot move forward
if(arr[0] == 0 && n > 1) return -1;
while(i < n) {
int x = arr[i]; // * Maximum steps we can jump from current index
int maxEle = -1, maxIdx = i;
// * If we can directly jump to or beyond the last element
if(i + x >= n - 1) return jumps + 1;
jumps++; // * Increment the jump count
// * Find the index with MAX ele to jump to within the current jump range
for(int j = i + 1; j <= i + x; j++) {
if(arr[j] > maxEle){
maxEle = arr[j];
maxIdx = j;
}
}
// * If no progress can be made, return -1
if(maxIdx == i) return -1;
i = maxIdx; // * Jump to the best index
}
return jumps;
}