返回介绍

solution / 2000-2099 / 2029.Stone Game IX / README_EN

发布于 2024-06-17 01:03:11 字数 4987 浏览 0 评论 0 收藏 0

2029. Stone Game IX

中文文档

Description

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.

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

Solutions

Solution 1

class Solution:
  def stoneGameIX(self, stones: List[int]) -> bool:
    def check(c):
      if c[1] == 0:
        return False
      c[1] -= 1
      turn = 1 + min(c[1], c[2]) * 2 + c[0]
      if c[1] > c[2]:
        turn += 1
        c[1] -= 1
      return turn % 2 == 1 and c[1] != c[2]

    c = [0] * 3
    for s in stones:
      c[s % 3] += 1
    c1 = [c[0], c[2], c[1]]
    return check(c) or check(c1)
class Solution {
  public boolean stoneGameIX(int[] stones) {
    int[] c = new int[3];
    for (int s : stones) {
      ++c[s % 3];
    }
    int[] t = new int[] {c[0], c[2], c[1]};
    return check(c) || check(t);
  }

  private boolean check(int[] c) {
    if (c[1] == 0) {
      return false;
    }
    --c[1];
    int turn = 1 + Math.min(c[1], c[2]) * 2 + c[0];
    if (c[1] > c[2]) {
      --c[1];
      ++turn;
    }
    return turn % 2 == 1 && c[1] != c[2];
  }
}
class Solution {
public:
  bool stoneGameIX(vector<int>& stones) {
    vector<int> c(3);
    for (int s : stones) ++c[s % 3];
    vector<int> t = {c[0], c[2], c[1]};
    return check(c) || check(t);
  }

  bool check(vector<int>& c) {
    if (c[1] == 0) return false;
    --c[1];
    int turn = 1 + min(c[1], c[2]) * 2 + c[0];
    if (c[1] > c[2]) {
      --c[1];
      ++turn;
    }
    return turn % 2 == 1 && c[1] != c[2];
  }
};
func stoneGameIX(stones []int) bool {
  check := func(c [3]int) bool {
    if c[1] == 0 {
      return false
    }
    c[1]--
    turn := 1 + min(c[1], c[2])*2 + c[0]
    if c[1] > c[2] {
      c[1]--
      turn++
    }
    return turn%2 == 1 && c[1] != c[2]
  }
  c := [3]int{}
  for _, s := range stones {
    c[s%3]++
  }
  return check(c) || check([3]int{c[0], c[2], c[1]})
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文