-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy path55_JumpGame.cpp
More file actions
37 lines (34 loc) · 782 Bytes
/
55_JumpGame.cpp
File metadata and controls
37 lines (34 loc) · 782 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
28
29
30
31
32
33
34
35
36
37
/*
* @Author: xuezaigds@gmail.com
* @Last Modified time: 2016-06-27 13:06:01
*/
class Solution {
public:
bool canJump(vector<int>& nums) {
/*
The main idea is to see if current element can be
reached by previous max jump.
If not, return false. If true, renew the max jump.
*/
int length = nums.size();
int max_distance = nums[0];
for(int i=0; i<length; i++){
if(max_distance >= length-1){
return true;
}
if(max_distance >= i){
max_distance = max(max_distance, i+nums[i]);
}
else{
return false;
}
}
return true;
}
};
/*
[0]
[2,3,1,1,4]
[3,2,1,0,4]
[1,3,5,0,0,0,0,0]
*/