Skip to content

Commit

Permalink
Create 2029.Stone-Game-IX.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Oct 3, 2021
1 parent 3fd24ad commit 05e69a4
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Others/2029.Stone-Game-IX/2029.Stone-Game-IX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Solution {
public:
bool stoneGameIX(vector<int>& stones)
{
vector<int>count(3);
for (int x: stones)
count[x%3]+=1;

auto temp = count;
if (temp[1]>0)
{
temp[1]-=1;
if (!win(temp, 1, 1))
return true;
}

temp = count;
if (temp[2]>0)
{
temp[2]-=1;
if (!win(temp, 2, 1))
return true;
}

return false;
}

bool win(vector<int>&count, int sum, int turn)
{
if (count[0]+count[1]+count[2]==0)
{
if (turn == 1)
return true;
else
return false;
}

if (count[0]>0)
{
count[0]-=1;
return 1-win(count, sum, 1-turn);
}

if (sum%3==1)
{
if (count[1]>0)
{
count[1]--;
return 1-win(count, sum+1, 1-turn);
}
else
return false;
}
else
{
if (count[2]>0)
{
count[2]--;
return 1 - win(count, sum+2, 1-turn);
}
else
return false;
}
}
};

0 comments on commit 05e69a4

Please sign in to comment.