forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path45-Jump-Game-II.cpp
37 lines (32 loc) · 873 Bytes
/
45-Jump-Game-II.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
36
37
/*
Given int array, determine min jumps to reach last index
Ex. nums = [2,3,1,1,4] -> 2, index 0 to 1 to last
Greedy: At each point, determine furthest reachable, jump to it
Time: O(n)
Space: O(1)
*/
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size();
int result = 0;
int i = 0;
while (i < n - 1) {
if (i + nums[i] >= n - 1) {
result++;
break;
}
int maxIndex = i + 1;
int maxValue = 0;
for (int j = i + 1; j < i + 1 + nums[i]; j++) {
if (j + nums[j] > maxValue) {
maxIndex = j;
maxValue = j + nums[j];
}
}
i = maxIndex;
result++;
}
return result;
}
};