-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.cpp
More file actions
56 lines (44 loc) · 916 Bytes
/
JumpGame.cpp
File metadata and controls
56 lines (44 loc) · 916 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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
vector<int> store(nums.size(),-1);
return helperCanJump(nums, nums.size()-1, store);
}
int helperCanJump(vector<int>& nums, int idx, vector<int>& store) {
if (nums.size() == 1)
return 1;
if (idx == 0 && nums[idx] > 0) {
store[idx] = 1;
return 1;
}
else {
for (int i = idx-1; i >= 0; --i)
{
if (nums[i] >= idx-i) {
if (store[i] == 1)
return 1;
else if (store[i] == 0) {}
else {
int res = helperCanJump(nums, idx-1, store);
store[idx-1] = res;
if (res == 1)
return 1;
}
}
}
return 0;
}
}
};
int main(int argc, char const *argv[])
{
Solution sol;
int a[] = {3,2,1,0,4};
vector<int> nums(a, a + sizeof(a) / sizeof(a[0]));
bool res = sol.canJump(nums);
cout << res << endl;
return 0;
}