-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-55-Jump_Game.cpp
56 lines (48 loc) · 1.43 KB
/
leetcode-55-Jump_Game.cpp
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 <cstdlib>
#include <vector>
bool canJump(std::vector<int>& nums) {
if(nums.size() == 1)
return true;
for(size_t i = 0; i<nums.size(); ++i)
{
if((nums.size()-1)<=i+nums[i])
return true;
int max = -1;
for(size_t j = 0; j<nums[i] && (i+j+1)<nums.size(); ++j)
{
int a = nums[i+j+1];
if(a == 0)
continue;
if((nums.size()-1)<=(i+j+1+a))
return true;
if(max == -1 || nums[max]<=nums[i+j+1])
{
for(size_t k = 1; k<=a; ++k)
{
if(max == -1 && nums[i+j+1+k] != 0){
max = i+j+1;
} else if(max != -1 && nums[max]<=nums[i+j+1] && nums[i+j+1+k] != 0) {
max = i+j+1;
}
}
}
}
if(max == -1)
return false;
i = (--max);
}
return false;
}
int main(){
// std::vector<int> vec = {2,3,1,1,4};
// std::vector<int> vec = {3,2,1,0,4};
// std::vector<int> vec = {2,0,0};
// std::vector<int> vec = {5,9,3,2,1,0,2,3,3,1,0,0};
// std::vector<int> vec = {4,2,0,0,1,1,4,4,4,0,4,0};
// std::vector<int> vec = {1,3,2};
std::vector<int> vec = {1,1,2,2,0,1,1};
bool res = canJump(vec);
std::cout << res << std::endl;
return EXIT_SUCCESS;
}