-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path[TLE]leetcode55_jump_game.cpp
83 lines (67 loc) · 1.88 KB
/
[TLE]leetcode55_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @file leetcode55_jump_game.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/05/28 15:55
* @brief https://leetcode.com/problems/jump-game/
*
* NOTE: Although correct, both the backtrack and DP solution is TLE on leetcode.
* Better solution needs to be done.
**/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
bool canJump(vector<int>& nums) {
size_t len = nums.size();
std::vector<std::vector<bool>> reachable;
reachable.resize(len);
for (size_t i = 0; i < len; ++i) {
reachable[i].resize(len);
for (int j = 0; j <= nums[i] && (i + j < len); ++j) {
reachable[i][i + j] = true;
}
}
if (reachable[0][len-1]) {
return true;
}
for (size_t i = 1; i < len; ++i) {
for (size_t j = 1; j < i; ++j) {
if (reachable[0][j] && reachable[j][i]) {
reachable[0][i] = true;
}
}
if (!reachable[0][i]) {
return false;
}
}
return reachable[0][len - 1];
}
};
int main() {
int n;
std::vector<int> nums;
while (1) {
n = 0;
std::cout << "Number of elements (Ctrl-C to exit): ";
std::cin >> n;
if (!cin >> n) {
return 0;
}
if (n <= 0) {
return 0;
}
std::cout << n << " integers: " << std::endl;
nums.clear();
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
nums.push_back(ele);
}
Solution solution;
auto ret = solution.canJump(nums);
std::cout << (ret ? "true": "false") << std::endl;
}
return 0;
}