-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJump_Game_II.cpp
More file actions
56 lines (50 loc) · 1.46 KB
/
Jump_Game_II.cpp
File metadata and controls
56 lines (50 loc) · 1.46 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//naive approach
// int jump(vector<int>& nums) {
// int mx, mi; // variables to keep track of max value and index of max value
// int i = 0, start, counter = 0;
// int sz = nums.size();
// while(i < nums.size()) {
// counter++;
// if(nums.size() < 2) {
// return 0;
// }
// if(i + nums[i] >= sz-1) { //current reaches end
// return counter;
// }
// else { //current doesn't reach, choose largest of next i terms
// mx = *(nums.begin() + i);
// mi = i;
// for(int j = i; j <= i + nums[i]; j++) {
// // cout << i << " " << j << nums[i] << endl;
// if(nums[j] >= mx) {
// mx = nums[j];
// mi = j;
// }
// }
// i = mi;
// }
// // cout << i << endl;
// }
// return counter;
// }
int jump(vector<int>& nums) {
int n = nums.size();
if(n < 2) return 0;
int maxPosition = nums[0], maxSteps = nums[0];
int jumps = 1;
for(int i = 0; i < n; i++) {
if(maxSteps < i) { //reached end of currently reachable window
jumps++;
maxSteps = maxPosition;
}
maxPosition = max(maxPosition, nums[i] + i);
}
return jumps;
}
int main() {
vector<int> nums = {1, 2, 1, 1, 1};
cout << jump(nums) << endl;
}