Skip to content

Commit 05e69a4

Browse files
authored
Create 2029.Stone-Game-IX.cpp
1 parent 3fd24ad commit 05e69a4

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public:
3+
bool stoneGameIX(vector<int>& stones)
4+
{
5+
vector<int>count(3);
6+
for (int x: stones)
7+
count[x%3]+=1;
8+
9+
auto temp = count;
10+
if (temp[1]>0)
11+
{
12+
temp[1]-=1;
13+
if (!win(temp, 1, 1))
14+
return true;
15+
}
16+
17+
temp = count;
18+
if (temp[2]>0)
19+
{
20+
temp[2]-=1;
21+
if (!win(temp, 2, 1))
22+
return true;
23+
}
24+
25+
return false;
26+
}
27+
28+
bool win(vector<int>&count, int sum, int turn)
29+
{
30+
if (count[0]+count[1]+count[2]==0)
31+
{
32+
if (turn == 1)
33+
return true;
34+
else
35+
return false;
36+
}
37+
38+
if (count[0]>0)
39+
{
40+
count[0]-=1;
41+
return 1-win(count, sum, 1-turn);
42+
}
43+
44+
if (sum%3==1)
45+
{
46+
if (count[1]>0)
47+
{
48+
count[1]--;
49+
return 1-win(count, sum+1, 1-turn);
50+
}
51+
else
52+
return false;
53+
}
54+
else
55+
{
56+
if (count[2]>0)
57+
{
58+
count[2]--;
59+
return 1 - win(count, sum+2, 1-turn);
60+
}
61+
else
62+
return false;
63+
}
64+
}
65+
};

0 commit comments

Comments
 (0)