forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGameII.cpp
More file actions
27 lines (27 loc) · 750 Bytes
/
JumpGameII.cpp
File metadata and controls
27 lines (27 loc) · 750 Bytes
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
class Solution {
public:
int jump(int A[], int n) {
int rightmost = 0;
int start = 0;
int* minstep = new int[n];
for(int i=1;i<n;i++) {
minstep[i] = 0x7fffffff;
}
minstep[0] = 0;
while(start<=rightmost) {
if(start + A[start] > rightmost) {
rightmost = start + A[start];
if(rightmost>n-1) {
rightmost = n-1;
}
for(int i=start+1;i<=rightmost;i++) {
if (minstep[i] > minstep[start]+1) {
minstep[i] = minstep[start]+1;
}
}
}
start++;
}
return minstep[n-1];
}
};