2038. Remove Colored Pieces if Both Neighbors are the Same Color

题目:

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'

思路:

这题看上去花里胡哨,感觉好像要用dp,但是其实仔细观察,看一眼60%的通过率,再模拟一下题目的思路,只是要找出连续三个及以上A的数量和连续三个及以上B的数量即可解答。举个例子,“AAABABB”,则Alice只能操作前面三个连续AAA中的中间一个A,并且之后变成了“AABABB”,连续的两个A无法操作,即消去的操作不会造成出现新的三个连续A。那么我们只要记录下连续的三个以上的A,将每一个这种情况减去头尾的两个A,即减2,就是Alice总的可以操作的量a;同理获得Bob可以操作的量b,返回a > b即可。因为Alice是先手,如果a和b相等,还是Alice输。

代码:

class Solution {
public:
    bool winnerOfGame(string colors) {
        if (colors.size() <= 2) 
            false;
        int cur = 1, a = 0, b = 0;
        bool A = colors[0] == 'A' ? true : false;
        for (int i = 1; i < colors.size(); i++) {
            if (colors[i] == 'A') {
                if (A) {
                    cur++;
                } else {
                    b += max(0, cur - 2);
                    cur = 1;
                    A = true;
                }
            } else {
                if (A) {
                    a += max(0, cur - 2);
                    cur = 1;
                    A = false;
                } else {
                    cur++;
                }
            }
        }
        if(A) a += max(0, cur - 2);
        else b += max(0, cur - 2);
        
        return a > b;
    }
};

07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值