-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0055_Jump_Game.cpp
More file actions
36 lines (30 loc) · 806 Bytes
/
Copy path0055_Jump_Game.cpp
File metadata and controls
36 lines (30 loc) · 806 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
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
int i, cur = 0, next = 0, n = nums.size();
if(n <= 1) return true;
for(i = 0; i < n; i++){
if(cur < i){
cur = next;
if(cur < i) return false;
}
next = max(next, nums[i] + i);
if(next >= n - 1) return true;
}
return false;
}
};
int main(){
Solution solve;
//vector<int> input = {2,3,1,1,4};
//vector<int> input = {3,2,1,0,4};
vector<int> input = {0,4};
cout << "Input: " << endl;
for(int i = 0; i < input.size(); i++) cout << input[i] << " ";
cout << endl;
cout << "Output: " << solve.canJump(input) << endl;
return 0;
}