-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0464.cpp
More file actions
31 lines (26 loc) · 759 Bytes
/
0464.cpp
File metadata and controls
31 lines (26 loc) · 759 Bytes
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
class Solution {
public:
unordered_map<int, bool> memo;
bool canWin(int mask, int mx, int total, int desired) {
if (memo.count(mask))
return memo[mask];
for (int i = 0; i < mx; ++i) {
if (!(mask & (1 << i))) {
int next = mask | (1 << i);
if (i + 1 >= desired ||
!canWin(next, mx, total + i + 1, desired - (i + 1))) {
return memo[mask] = true;
}
}
}
return memo[mask] = false;
}
bool canIWin(int maxChoosableInteger, int desiredTotal) {
int sum = (maxChoosableInteger * (maxChoosableInteger + 1)) / 2;
if (sum < desiredTotal)
return false;
if (desiredTotal <= 0)
return true;
return canWin(0, maxChoosableInteger, 0, desiredTotal);
}
};