-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path464. Can I Win.cpp
269 lines (235 loc) · 8.21 KB
/
464. Can I Win.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//brute force
//https://leetcode.com/problems/can-i-win/discuss/155321/From-Brute-Force-to-Top-down-DP
//TLE
//5 / 54 test cases passed.
class Solution {
public:
bool canWin(vector<bool>& choosable, int target){
if(none_of(choosable.begin(), choosable.end(),
[](const bool b){return b;})){
return false;
}
if(target <= 0){
return false;
}
int maxChoosable;
for(int i = choosable.size()-1; i >= 1; i--){
if(choosable[i]){
maxChoosable = i;
break;
}
}
if(target <= maxChoosable){
//we can go to the number >= target
return true;
}
//once we can make the next player lose, we win
for(int i = 1; i < choosable.size(); i++){
if(choosable[i]){
bool nextCanWin;
choosable[i] = false;
nextCanWin = canWin(choosable, target-i);
choosable[i] = true;
if(!nextCanWin) return true;
}
}
//the next player will win anyway
return false;
}
bool canIWin(int maxChoosableInteger, int desiredTotal) {
if(desiredTotal <= maxChoosableInteger){
return true;
}
if((1+maxChoosableInteger)*maxChoosableInteger/2 < desiredTotal){
return false;
}
//0 for padding
vector<bool> choosable(maxChoosableInteger+1, true);
return canWin(choosable, desiredTotal);
}
};
//top-down DP
//https://leetcode.com/problems/can-i-win/discuss/155321/From-Brute-Force-to-Top-down-DP
//Runtime: 872 ms, faster than 5.08% of C++ online submissions for Can I Win.
//Memory Usage: 71.4 MB, less than 12.41% of C++ online submissions for Can I Win.
class Solution {
public:
unordered_map<string, bool> memo;
bool canWin(vector<bool>& choosable, int target){
if(none_of(choosable.begin(), choosable.end(),
[](const bool b){return b;})){
return false;
}
if(target <= 0){
return false;
}
int maxChoosable;
//choosable[0] is just for padding, so we end at 1!
for(int i = choosable.size()-1; i >= 1; i--){
if(choosable[i]){
maxChoosable = i;
break;
}
}
if(target <= maxChoosable){
//we can go to the number >= target
return true;
}
/*
don't need to track "target",
since target can be calculated from "desiredTotal" and
current "choosable"!
*/
string key = string(choosable.begin(), choosable.end());
if(memo.find(key) != memo.end()){
return memo[key];
}
//once we can make the next player lose, we win
//choosable[0] is just for padding, so we start from 1!
for(int i = 1; i < choosable.size(); i++){
if(choosable[i]){
bool nextCanWin;
choosable[i] = false;
nextCanWin = canWin(choosable, target-i);
choosable[i] = true;
if(!nextCanWin) return memo[key] = true;
}
}
//the next player will win anyway
return memo[key] = false;
}
bool canIWin(int maxChoosableInteger, int desiredTotal) {
if(desiredTotal <= maxChoosableInteger){
return true;
}
if((1+maxChoosableInteger)*maxChoosableInteger/2 < desiredTotal){
return false;
}
//0 for padding
vector<bool> choosable(maxChoosableInteger+1, true);
return canWin(choosable, desiredTotal);
}
};
//top-bottom dp, use vector<int>
//https://leetcode.com/problems/can-i-win/discuss/95320/Clean-C%2B%2B-beat-98.4-DFS-with-early-termination-check-(detailed-explanation)
//Runtime: 48 ms, faster than 63.89% of C++ online submissions for Can I Win.
//Memory Usage: 149.1 MB, less than 7.81% of C++ online submissions for Can I Win.
class Solution {
public:
vector<int> memo;
// int memo[1<<20];
int canWin(int maxChoosableInteger, int state, int target){
/*
state: can be thought as bits s.t.
ith bit represents integer i+1 is chosen or not
*/
// cout << state << endl;
if(memo[state] != 0){
return memo[state] == 1;
}
if(target <= 0){
return false;
}
//once we can make the next player lose, we win
/*
i's range is [0, maxChoosableInteger-1],
but it represents for the numbers to be chosen: [1, maxChoosableInteger]
*/
for(int i = 0; i < maxChoosableInteger; i++){
/*
state&(1<<i): ith bit of state, represents for i+1 is chosen or not
state|(1<<i): saying that ith bit is already chosen
*/
if(!(state&(1<<i)) && !canWin(maxChoosableInteger, state|(1<<i), target-(i+1))){
memo[state] = 1;
return true;
}
}
//the next player will win anyway
memo[state] = -1;
return false;
}
bool canIWin(int maxChoosableInteger, int desiredTotal) {
if(desiredTotal <= maxChoosableInteger){
return true;
}
int sum = (1+maxChoosableInteger)*maxChoosableInteger/2;
/*
can't make up desiredTotal using [1, maxChoosableInteger],
so neither can win
*/
if(sum < desiredTotal){
return false;
}else if(sum == desiredTotal){
/*
we will reach desiredTotal at last step,
so the player who choose the last integer will win,
i.e. when maxChoosableInteger is odd, 1st player will win
*/
return maxChoosableInteger % 2;
}
memo = vector<int>(1<<20, 0);
return canWin(maxChoosableInteger, 0, desiredTotal);
}
};
//top-bottom dp, use int array
//Runtime: 16 ms, faster than 90.35% of C++ online submissions for Can I Win.
//Memory Usage: 10.5 MB, less than 93.88% of C++ online submissions for Can I Win.
class Solution {
public:
// vector<int> memo;
int memo[1<<20];
int canWin(int maxChoosableInteger, int state, int target){
/*
state: can be thought as bits s.t.
ith bit represents integer i+1 is chosen or not
*/
// cout << state << endl;
if(memo[state] != 0){
return memo[state] == 1;
}
if(target <= 0){
return false;
}
//once we can make the next player lose, we win
/*
i's range is [0, maxChoosableInteger-1],
but it represents for the numbers to be chosen: [1, maxChoosableInteger]
*/
for(int i = 0; i < maxChoosableInteger; i++){
/*
state&(1<<i): ith bit of state, represents for i+1 is chosen or not
state|(1<<i): saying that ith bit is already chosen
*/
if(!(state&(1<<i)) && !canWin(maxChoosableInteger, state|(1<<i), target-(i+1))){
memo[state] = 1;
return true;
}
}
//the next player will win anyway
memo[state] = -1;
return false;
}
bool canIWin(int maxChoosableInteger, int desiredTotal) {
if(desiredTotal <= maxChoosableInteger){
return true;
}
int sum = (1+maxChoosableInteger)*maxChoosableInteger/2;
/*
can't make up desiredTotal using [1, maxChoosableInteger],
so neither can win
*/
if(sum < desiredTotal){
return false;
}else if(sum == desiredTotal){
/*
we will reach desiredTotal at last step,
so the player who choose the last integer will win,
i.e. when maxChoosableInteger is odd, 1st player will win
*/
return maxChoosableInteger % 2;
}
// memo = vector<int>(1<<20, 0);
return canWin(maxChoosableInteger, 0, desiredTotal);
}
};