-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.cpp
More file actions
33 lines (30 loc) · 763 Bytes
/
JumpGame.cpp
File metadata and controls
33 lines (30 loc) · 763 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
#include<iostream>
#include<vector>
using namespace std;
/*
Greedy: Time Complicity: O(n), Space Complicity: O(1)
Always calculate the farest index the current index
could jump to.
*/
class Solution{
public:
bool canJump(vector<int>& nums) {
int farest = 0, length = nums.size();
bool canReach = false;
for(int i = 0; i <= farest && i < length; i++){
farest = max(i + nums[i], farest);
if(farest > length - 2){
canReach = true;
break;
}
}
return canReach;
}
};
int main(){
int input[] = {0};//{3,2,1,0,4};
vector<int> nums(input, input + 1);
Solution solution;
cout << solution.canJump(nums) << endl;
return 0;
}