-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpGame.cpp
More file actions
43 lines (41 loc) · 901 Bytes
/
Copy pathjumpGame.cpp
File metadata and controls
43 lines (41 loc) · 901 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 <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
if (nums.size() == 1) {
return true;
}
int start = nums.size() - 2;
int end = nums.size() - 1;
bool foundStart;
while (start >= 0) {
foundStart = false;
if (end <= nums[start] + start) {
foundStart = true;
end = start;
start = end - 1;
} else {
start--;
}
}
if (foundStart && start == -1) {
return true;
}
return false;
}
};
int main() {
vector<int> nums;
int num;
while (cin >> num && num != -1) {
nums.push_back(num);
}
Solution solution;
if (solution.canJump(nums)) {
cout << "Can jump" << endl;
} else {
cout << "Cannot jump" << endl;
}
}