-
Notifications
You must be signed in to change notification settings - Fork 3
/
45.cpp
34 lines (33 loc) · 787 Bytes
/
45.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
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, INT_MAX);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
int step = nums[i];
for (int j = 1; j <= step; ++j) {
if (i + j >= n) break;
dp[i + j] = min(dp[i + j], dp[i] + 1);
}
}
return dp[n - 1];
}
};
//v2
class Solution {
public:
int jump(vector<int>& nums) {
int near = 0;
int farthest = 0;
int steps = 0;
for (int i = 0; i < nums.size(); i++) {
if (i > near) {
steps++;
near = farthest;
}
farthest = max(farthest, i + nums[i]);
}
return steps;
}
};