-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path55. Jump Game.cpp
197 lines (164 loc) · 4.92 KB
/
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//TLE
class Solution {
public:
bool canJump(vector<int>& nums) {
int N = nums.size();
vector<bool> dp(N, false);
dp[N-1] = true;
for(int i = N-1; i >= 0; i--){
if(nums[i] >= 1){
//can reach to one of dp[i+j] which is true
for(int j = 1; j <= nums[i] && i+j < N; j++){
if(dp[i+j]){
dp[i] = true;
break;
}
}
}
}
for(int i = 0; i < N; i++){
if(nums[i] >= 1){
//can reach to one of dp[i+j] which is true
for(int j = 1; j <= nums[i] && i-j >= 0; j++){
if(dp[i-j]){
dp[i] = true;
break;
}
}
}
}
return dp[0];
}
};
//TLE
//74 / 75 test cases passed.
//Graph
class Solution {
public:
bool canJump(vector<int>& nums) {
map<int, vector<int>> graph;
int n = nums.size();
for(int i = 0; i < n; i++){
// for(int j = max(0, i-nums[i]); j <= min(n-1, i+nums[i]); j++){
for(int j = i; j <= min(n-1, i+nums[i]); j++){
graph[i].push_back(j);
}
}
queue<int> q;
q.push(0);
vector<bool> visited(n, false);
visited[0] = true;
while(!q.empty()){
int cur = q.front(); q.pop();
for(int next : graph[cur]){
if(!visited[next]){
visited[next] = true;
q.push(next);
}
}
}
return visited[n-1];
}
};
//Approach 1: Backtracking
//TLE
//time: O(2^n), space: O(n)
class Solution {
public:
bool canJumpFromPos(vector<int>& nums, int pos){
int N = nums.size();
if(pos == N-1) return true;
int furthestJump = min(pos+nums[pos], N-1);
// for(int nextPos = pos+1; nextPos <= furthestJump; nextPos++){
for(int nextPos = furthestJump; nextPos > pos; nextPos--){
if(canJumpFromPos(nums, nextPos)){
return true;
}
}
return false;
};
bool canJump(vector<int>& nums) {
return canJumpFromPos(nums, 0);
}
};
//Approach 2: Dynamic Programming Top-down(backtracking with memorization)
//TLE
//time: O(n^2), space: O(n)
class Solution {
public:
enum Index {
GOOD, BAD, UNKNOWN
};
vector<Index> memo;
bool canJumpFromPos(vector<int>& nums, int pos){
int N = nums.size();
if(memo[pos] != Index::UNKNOWN){
return memo[pos] == GOOD ? true : false;
}
int furthestJump = min(pos+nums[pos], N-1);
// for(int nextPos = pos+1; nextPos <= furthestJump; nextPos++){
for(int nextPos = furthestJump; nextPos > pos; nextPos--){
if(canJumpFromPos(nums, nextPos)){
memo[pos] = Index::GOOD;
return true;
}
}
memo[pos] = Index::BAD;
return false;
};
bool canJump(vector<int>& nums) {
int N = nums.size();
memo = vector<Index>(N);
for(int i = 0; i < N-1; i++){
memo[i] = Index::UNKNOWN;
}
memo[N-1] = Index::GOOD;
return canJumpFromPos(nums, 0);
}
};
//Approach 3: Dynamic Programming Bottom-up
//Runtime: 516 ms, faster than 14.01% of C++ online submissions for Jump Game.
//Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Jump Game.
//time: O(n^2), space: O(n)
class Solution {
public:
enum Index {
GOOD, BAD, UNKNOWN
};
vector<Index> memo;
bool canJump(vector<int>& nums) {
int N = nums.size();
memo = vector<Index>(N);
for(int i = 0; i < N-1; i++){
memo[i] = Index::UNKNOWN;
}
memo[N-1] = Index::GOOD;
for(int pos = N-2; pos >= 0; pos--){
int furthestJump = min(pos+nums[pos], N-1);
for(int nextPos = furthestJump; nextPos > pos; nextPos--){
if(memo[nextPos] == Index::GOOD){
memo[pos] = Index::GOOD;
break;
}
}
}
return memo[0] == Index::GOOD;
}
};
//Approach 4: Greedy
//Runtime: 12 ms, faster than 73.33% of C++ online submissions for Jump Game.
//Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Jump Game.
//time: O(n), space: O(1)
class Solution {
public:
bool canJump(vector<int>& nums) {
int N = nums.size();
int lastGood = N-1;
for(int i = N-2; i >= 0; i--){
if(i + nums[i] >= lastGood){
lastGood = i;
}
}
return lastGood == 0;
}
};