LeetCode每日一题(2038. Remove Colored Pieces if Both Neighbors are the Same Color)

Alice和Bob玩一个基于颜色的游戏,他们轮流移除颜色为'A'或'B'的棋子。Alice只能移除两侧颜色均为'A'的'A'棋子,Bob则只能移除两侧颜色均为'B'的'B'棋子。如果一个玩家无法在回合中移动,则该玩家输掉游戏。给定棋子颜色序列,判断Alice是否能获胜。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There are n pieces arranged in a line, and each piece is colored either by ‘A’ or by ‘B’. You are given a string colors of length n where colors[i] is the color of the ith piece.

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

Alice is only allowed to remove a piece colored ‘A’ if both its neighbors are also colored ‘A’. She is not allowed to remove pieces that are colored ‘B’.
Bob is only allowed to remove a piece colored ‘B’ if both its neighbors are also colored ‘B’. He is not allowed to remove pieces that are colored ‘A’.
Alice and Bob cannot remove pieces from the edge of the line.
If a player cannot make a move on their turn, that player loses and the other player wins.
Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.

Example 1:

Input: colors = “AAABABB”
Output: true

Explanation:
AAABABB -> AABABB
Alice moves first.
She removes the second ‘A’ from the left since that is the only ‘A’ whose neighbors are both ‘A’.

Now it’s Bob’s turn.
Bob cannot make a move on his turn since there are no 'B’s whose neighbors are both ‘B’.
Thus, Alice wins, so return true.

Example 2:

Input: colors = “AA”
Output: false

Explanation:
Alice has her turn first.
There are only two 'A’s and both are on the edge of the line, so she cannot move on her turn.
Thus, Bob wins, so return false.

Example 3:

Input: colors = “ABBBBBBBAAA”
Output: false

Explanation:
ABBBBBBBAAA -> ABBBBBBBAA
Alice moves first.
Her only option is to remove the second to last ‘A’ from the right.

ABBBBBBBAA -> ABBBBBBAA
Next is Bob’s turn.
He has many options for which ‘B’ piece to remove. He can pick any.

On Alice’s second turn, she has no more pieces that she can remove.
Thus, Bob wins, so return false.

Constraints:

  • 1 <= colors.length <= 105
  • colors consists of only the letters ‘A’ and ‘B’

两个人无论怎么拿都影响不到对方的结果, 甚至连自己的结果也影响不了, 所以根本没有什么策略, 就是简单的对能拿的卡片进行计数。 能拿的卡片就是, 连续 n 张卡片, n > 2, 所能拿取的数量就是 n - 2, 因为 Alice 先拿取, 所以 Alice 所能拿取的卡片数量必须得大于 Bob 能拿取的数量,Alice 才能赢。



impl Solution {
    pub fn winner_of_game(colors: String) -> bool {
        let mut total_a = 0;
        let mut count_a = 0;
        let mut total_b = 0;
        let mut count_b = 0;
        for c in colors.chars() {
            if c == 'A' {
                if count_b > 0 {
                    total_b += (count_b - 2).clamp(0, i32::MAX);
                    count_b = 0;
                }
                count_a += 1;
                continue;
            }
            if count_a > 0 {
                total_a += (count_a - 2).clamp(0, i32::MAX);
                count_a = 0;
            }
            count_b += 1;
        }
        total_a += (count_a - 2).clamp(0, i32::MAX);
        total_b += (count_b - 2).clamp(0, i32::MAX);
        total_a > total_b
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值