Similar Problems

Similar Problems not available

Stone Game Ix - Leetcode Solution

Companies:

LeetCode:  Stone Game Ix Leetcode Solution

Difficulty: Medium

Topics: greedy math array  

Problem statement: Two players named Alex and Lee play a game with piles of stones. There are an odd number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].

The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.

Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.

Assuming Alex and Lee play optimally, return true if and only if Alex wins the game.

Example 1: Input: piles = [2,1,1,2,2,2] Output: true Explanation: Alex starts first, and can only take the first 2 or the last 2. Say he takes the first 2, then Lee can take the last 2. Then Alex takes the remaining 2 and wins 2 + 2 = 4 stones to Lee's 1 + 1 = 2. Example 2: Input: piles = [1,2,3,4,5,6,7] Output: false Explanation: Alex starts first, and can only take the first 1 or the last 7. Say he takes the first 1, then Lee can take any of the rest of the piles. Then Alex takes whatever Lee did not take, and Lee wins the remaining stones 4 + 3 + 2 + 1 = 10 to Alex's 1 + 5 + 6 + 7 = 19.

Constraints: 1 <= piles.length <= 10^5 1 <= piles[i] <= 10^4 sum(piles) is odd.

Solution: We can solve this problem using the Nim Game theory. In the Nim game, we have N heaps of stones, and both players alternate turns. On each turn, a player can remove one or more stones from one heap. The player who takes the last stone wins.

In this problem, we have the same rules but with only one difference, and that is the number of piles is odd. That means one player will always have one extra turn compared to the other player.

Let us assume that this player is Alex (the first player). Alex aims to win the game. To do that, he needs to choose the optimal piles to take. However, he can't win the game if the total number of stones is even.

So, we need to count the number of stones that are even and odd. Let's assume the total number of even piles is X and the total number of odd piles is Y. Then, we need to consider the following cases:

1- If X is odd and Y is even, Alex wins. 2- If X is odd and Y is odd, Alex wins if he has more even piles than Lee. 3- If X is even and Y is odd, Alex wins if he has more odd piles than Lee. 4- If X is even and Y is even, Alex loses So, we need to implement the above algorithm to check for who wins the game.

Code:

class Solution { public: bool stoneGameIX(vector<int>& piles) { int n = piles.size(); int X = 0, Y = 0, Z = 0; for (int i = 0; i < n; ++i) { if (piles[i] % 3 == 0) ++Z; else if (piles[i] % 3 == 1) ++X; else ++Y; } if (X > Y) swap(X, Y); int res = false; if (X > 0 && Y > 0) res = true; if (Z == 0) { if ((X - Y) % 2 != 0) res = true; } else { if ((X - Y) % 2 != 0) res = true; else { if (X > 0 && Y > 0) res = true; } } return res; } };

Time Complexity: O(n) Space Complexity: O(1)

Explanation: In the above code, we first count the number of even piles (X), odd piles (Y), and multiples of 3 (Z). Then, we swap X and Y if X > Y. We need to do that because Alex always starts first, so if X > Y, he can choose an even pile to start with and win.

Then, we check for the four cases that we discussed before. We need to check if Alex has at least one even and one odd pile to start with. If not, he can't win the game.

If Z == 0, that means there are no multiples of 3 piles. In this case, if (X - Y) % 2 != 0, Alex wins. Because if X > Y (after swapping), he can choose an even pile and remove it. Then, Lee will have to choose an odd pile to remove, and so on. Consequently, Alex will always have the last turn and win.

If Z > 0, that means there are multiples of 3 piles. In this case, if (X - Y) % 2 != 0, Alex wins. Because he can choose a multiple of 3 pile and remove it, then Lee can't do anything but remove either an odd or even pile. Consequently, Alex will have the last turn again and win.

If (X - Y) % 2 == 0, that means they have the same number of even and odd piles. In this case, Alex can't win unless he has at least one even and one odd pile to start with.

Finally, we return res as a boolean value representing whether Alex wins or not.

Stone Game Ix Solution Code

1