-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path464.cpp
More file actions
52 lines (48 loc) · 1.06 KB
/
Copy path464.cpp
File metadata and controls
52 lines (48 loc) · 1.06 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
#include "common.h"
using namespace std;
class Solution {
public:
map<int, bool> memo;
int used;
int maxChoosableInteger;
bool canIWin(int maxChoosableInteger, int desiredTotal) {
int sum = maxChoosableInteger * (maxChoosableInteger + 1) / 2;
if (desiredTotal < 2)
return true;
else if (sum < desiredTotal)
return false;
else if (sum == desiredTotal)
return maxChoosableInteger % 2;
this->maxChoosableInteger = maxChoosableInteger;
return help(desiredTotal);
}
bool help(int desire) {
if (memo.count(used)) {
return memo[used];
}
bool res = false;
for (size_t i = 1; i <= maxChoosableInteger; i++) {
if ((used >> i) & 1) {
continue;
}
if (i >= desire) {
res = true;
break;
}
used |= 1 << i;
bool ret = help(desire - i);
used &= ~(1 << i);
if (ret == false) {
res = true;
break;
}
}
memo[used] = res;
return res;
}
};
int main() {
Solution s;
cout << s.canIWin(18, 79) << endl;
return 0;
}