Skip to content

2029. Stone Game IX 👎

  • Time: $O(n)$
  • Space: $O(3) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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;
  }
}
1
2
3
4
5
6
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