-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55. Jump Game.cpp
More file actions
52 lines (45 loc) · 1.11 KB
/
55. Jump Game.cpp
File metadata and controls
52 lines (45 loc) · 1.11 KB
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
class Solution {
public:
bool canJump(vector<int>& nums) {
int cur = 0;
int furthest = 0;
for (auto i = 0; i < nums.size() - 1; ++i)
{
furthest = max(furthest, i + nums[i]);
if (i == cur)
{
if (furthest > i)
{
cur = furthest;
}
else
{
return false;
}
}
}
return furthest >= nums.size() - 1;
}
};
// Dynamic programming
// table[i] = or (table[pre] >= i - pre)
// O(N ^ 2)
class Solution2 {
public:
bool canJump(vector<int>& nums) {
vector<bool>table(nums.size(), false);
table[0] = true;
for(auto i = 1; i < nums.size(); ++i)
{
for(auto j = 0; j < i; ++j)
{
if (table[j] && nums[j] >= i - j)
{
table[i] = true;
break;
}
}
}
return table[nums.size() - 1];
}
};