-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanIWin.cpp
More file actions
29 lines (27 loc) · 1007 Bytes
/
canIWin.cpp
File metadata and controls
29 lines (27 loc) · 1007 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
// Source: https://leetcode.com/problems/can-i-win/
// Author: Miao Zhang
// Date: 2021-02-14
class Solution {
public:
bool canIWin(int maxChoosableInteger, int desiredTotal) {
int sums = maxChoosableInteger * (maxChoosableInteger + 1) / 2;
if (sums < desiredTotal) return false;
if (desiredTotal <= 0) return true;
visited_ = vector<char>(1 << maxChoosableInteger, 0);
return canWin(maxChoosableInteger, desiredTotal, 0);
}
private:
vector<char> visited_;
bool canWin(int maxChoosableInteger, int desiredTotal, int state) {
if (desiredTotal <= 0) return false;
if (visited_[state]) return visited_[state] == 1;
for (int i = 0; i < maxChoosableInteger; i++) {
if (state & (1 << i)) continue;
if (!canWin(maxChoosableInteger, desiredTotal - (i + 1), state | (1 << i))) {
return visited_[state] = 1;
}
}
visited_[state] = -1;
return false;
}
};