2029. Stone Game IX LeetCode Solution

In this guide, you will get 2029. Stone Game IX LeetCode Solution with the best time and space complexity. The solution to Stone Game IX problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Stone Game IX solution in C++
  4. Stone Game IX solution in Java
  5. Stone Game IX solution in Python
  6. Additional Resources
2029. Stone Game IX LeetCode Solution image

Problem Statement of Stone Game IX

Alice and Bob continue their games with stones. There is a row of n stones, and each stone has an associated value. You are given an integer array stones, where stones[i] is the value of the ith stone.
Alice and Bob take turns, with Alice starting first. On each turn, the player may remove any stone from stones. The player who removes a stone loses if the sum of the values of all removed stones is divisible by 3. Bob will win automatically if there are no remaining stones (even if it is Alice’s turn).
Assuming both players play optimally, return true if Alice wins and false if Bob wins.

Example 1:

Input: stones = [2,1]
Output: true
Explanation: The game will be played as follows:
– Turn 1: Alice can remove either stone.
– Turn 2: Bob removes the remaining stone.
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.

See also  859. Buddy Strings LeetCode Solution

Example 2:

Input: stones = [2]
Output: false
Explanation: Alice will remove the only stone, and the sum of the values on the removed stones is 2.
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.

Example 3:

Input: stones = [5,1,2,4,3]
Output: false
Explanation: Bob will always win. One possible way for Bob to win is shown below:
– Turn 1: Alice can remove the second stone with value 1. Sum of removed stones = 1.
– Turn 2: Bob removes the fifth stone with value 3. Sum of removed stones = 1 + 3 = 4.
– Turn 3: Alices removes the fourth stone with value 4. Sum of removed stones = 1 + 3 + 4 = 8.
– Turn 4: Bob removes the third stone with value 2. Sum of removed stones = 1 + 3 + 4 + 2 = 10.
– Turn 5: Alice removes the first stone with value 5. Sum of removed stones = 1 + 3 + 4 + 2 + 5 = 15.
Alice loses the game because the sum of the removed stones (15) is divisible by 3. Bob wins the game.

Constraints:

1 <= stones.length <= 105
1 <= stones[i] <= 104

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(3) = O(1)

2029. Stone Game IX LeetCode Solution in C++

class Solution {
 public:
  bool stoneGameIX(vector<int>& stones) {
    vector<int> count(3);

    for (const int stone : stones)
      ++count[stone % 3];

    if (count[0] % 2 == 0)
      return min(count[1], count[2]) > 0;
    return abs(count[1] - count[2]) > 2;
  }
};
/* code provided by PROGIEZ */

2029. Stone Game IX LeetCode Solution in Java

class Solution {
  public boolean stoneGameIX(int[] stones) {
    int[] count = new int[3];

    for (final int stone : stones)
      ++count[stone % 3];

    if (count[0] % 2 == 0)
      return Math.min(count[1], count[2]) > 0;
    return Math.abs(count[1] - count[2]) > 2;
  }
}
// code provided by PROGIEZ

2029. Stone Game IX LeetCode Solution in Python

class Solution:
  def stoneGameIX(self, stones: list[int]) -> bool:
    count = collections.Counter(stone % 3 for stone in stones)
    if count[0] % 2 == 0:
      return min(count[1], count[2]) > 0
    return abs(count[1] - count[2]) > 2
# code by PROGIEZ

Additional Resources

See also  903. Valid Permutations for DI Sequence LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.