-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55_jump_game.cpp
More file actions
43 lines (36 loc) · 946 Bytes
/
55_jump_game.cpp
File metadata and controls
43 lines (36 loc) · 946 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
38
39
40
41
42
43
#include <vector>
#include <iterator>
#include <algorithm>
class Solution {
public:
bool canJump(std::vector<int>& nums) {
int gas = 0;
for(int i = 0; i < nums.size(); i++) {
if (gas < 0){
return false;
} else if (nums.at(i) > gas){
gas = nums.at(i);
}
gas -= 1;
}
return true;
}
/* recursion over time
bool canJump(vector<int>& nums) {
return canJump(nums, 0);
}
bool canJump(vector<int>& nums, int curIndex) {
if(curIndex == nums.size() - 1) {
return true;
}
if(nums.at(curIndex) == 0 || curIndex > nums.size() - 1) {
return false;
}
for(int i = 1; i <= nums.at(curIndex); i++) {
if(canJump(nums, curIndex+i)){
return true;
}
}
return false;
}*/
};