forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCan I Win.cpp
28 lines (28 loc) · 956 Bytes
/
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
class Solution {
private:
vector<vector<int>>dp;
bool backtrack(int curr,int &maxInt,int &desire,int mask,int player){
if(dp[player][mask]!=-1){
return dp[player][mask];
} else {
for(int i=1;i<=maxInt;i++){
int nmask=(1<<(i-1));
if((mask&nmask)==0){
if(curr+i>=desire or !backtrack(curr+i,maxInt,desire,mask+nmask,(player+1)%2)){
return dp[player][mask]=true;
}
}
}
return dp[player][mask]=false;
}
}
public:
bool canIWin(int maxChoosableInteger, int desiredTotal) {
dp=vector<vector<int>>(2,vector<int>(1<<maxChoosableInteger,-1));
if(maxChoosableInteger*(maxChoosableInteger+1)/2<desiredTotal){
return false;
}
int curr=0,mask=0;
return backtrack(curr,maxChoosableInteger,desiredTotal,mask,0);
}
};