-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathJump Game II.cpp
70 lines (48 loc) · 1.66 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Solution by Rahul Surana
***********************************************************
You are given a 0-indexed array of integers nums of length n.
You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i.
In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1].
The test cases are generated such that you can reach nums[n - 1].
***********************************************************
*/
class Solution {
public:
// vector<int> dp;
// top down
// int df(int x, int n, vector<int>& nums){
// if(n<=x) return 0;
// if(dp[x] != -1) return dp[x];
// // cout << x <<" "<<n <<"\n";
// int ans = 1e9;
// for(int i = 1; i <= nums[x]; i++){
// ans = min(ans,df(x+i,n,nums)+1);
// }
// return dp[x] = ans;
// }
int jump(vector<int>& nums) {
if(nums.size()==1) return 0;
// dp.resize(nums.size()+1,1e9);
// bottom up
int ans = 0, next = 0,end = 0;
for(int i = 0; i < nums.size()-1; ++i){
next = max(next,i+nums[i]);
if(next >= nums.size()-1){
++ans;
break;
}
if(end == i){
++ans;
end=next;
}
}
return ans;
// return dp[0];
// return df(0,nums.size()-1,nums);
}
};