forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path292.cpp
More file actions
50 lines (47 loc) · 1.21 KB
/
292.cpp
File metadata and controls
50 lines (47 loc) · 1.21 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
//method1:
class Solution {
public:
bool canWinNim(int n) {
return n%4;
}
};
//method2:
class Solution{
public:
bool canWinNim(int n){
vector<bool> dp(n+1,false);
dp[1]=dp[2]=dp[3]=true;
for(int i=4;i<=n;i++) dp[i]=!dp[i-1] || !dp[i-2] || !dp[i-3];
return dp[n];
}
};
//method3
class Solution{
public:
int maxMin(int n,vector<int>& table1,vector<int>& table2){
if(table1[n]!=0) return table1[n];
vector<int> tmp;
for(int i=1;i<4;i++){
if(n-i==0) return 1;
else if(n-i>0) tmp.push_back(minMax(n-i,table1,table2));
}
table1[n]=*max_element(tmp.begin(),tmp.end());
return table1[n];
}
int minMax(int n,vector<int>& table1,vector<int>& table2){
if(table2[n]!=0) return table2[n];
vector<int> tmp;
for(int i=1;i<4;i++){
if(n-i==0) return -1;
else if(n-i>0) tmp.push_back(maxMin(n-i,table1,table2));
}
table2[n]=*min_element(tmp.begin(),tmp.end());
return table2[n];
}
bool canWinNim(int n){
if(n>=134882061)
return n%4;
vector<int> table1(n+1,0),table2(n+1,0);
return maxMin(n,table1,table2)==1? true: false;
}
};