-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump_game_ii.cpp
More file actions
61 lines (50 loc) · 1.42 KB
/
jump_game_ii.cpp
File metadata and controls
61 lines (50 loc) · 1.42 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
57
58
59
60
61
class Solution {
public:
/**
* @param A: A list of lists of integers
* @return: An integer
*/
int jump(vector<int> A) {
// wirte your code here
int len = A.size();
if (len == 0) {
return 0;
}
vector<int> dp(len, INT_MAX);
dp[0] = 0;
for (int i = 1; i < len; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] < INT_MAX && j + A[j] >= i) {
dp[i] = min(dp[i], dp[j] + 1);
//dp[j] must be less than dp[j + 1 ...]
//so break here.
break;
}
}
}
return dp[len - 1];
}
int jump_ii(vector<int> A) {
// wirte your code here
int farest = 0;
int current = 0;
int jump = 0;
for (int i = 0; i < A.size() - 1; i++) {
if (farest < i) return -1;
farest = A[i] + i > farest ? A[i] + i : farest;
if (current == i) {
++jump;
current = farest;
}
if (farest >= A.size() -1) {
if (current < farest) {
jump++;
}
return jump;
}
}
if (farest >= A.size() - 1)
return jump;
return -1;
}
};