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


    Description

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Constraints:

    1 <= colors.length <= 10^5
    colors consists of only the letters 'A' and 'B'
    
    • 1
    • 2

    Solution

    Solved after hints…

    The number of removing alphabets doesn’t depend on the other player, but simply depends on the number of consecutive A or B. The number Alice could remove A is the number of consecutive A - 2, so does Bob.

    Calculate the number of moving alphabets, then compare.

    Time complexity: o ( n ) o(n) o(n)
    Space complexity: o ( 1 ) o(1) o(1)

    Code

    class Solution:
        def winnerOfGame(self, colors: str) -> bool:
            alice, bob = 0, 0
            consec_a, consec_b = -2, -2
            for i in colors:
                if i == 'A':
                    consec_b = -2
                    consec_a += 1
                    if consec_a > 0:
                        alice += 1
                elif i == 'B':
                    consec_a = -2
                    consec_b += 1
                    if consec_b > 0:
                        bob += 1
            return alice > bob
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Also a recursive and TLE version.

    class Solution:
        def winnerOfGame(self, colors: str) -> bool:
            memo = {}
            def helper(colors: str, cur_char: str) -> bool:
                if (colors, cur_char) in memo:
                    return memo[(colors, cur_char)]
                if cur_char == 'A':
                    for i in range(1, len(colors) - 1):
                        if colors[i - 1] == colors[i] == colors[i + 1] == 'A':
                            next_game = helper(colors[:i] + colors[i + 1:], 'B')
                            if next_game == True:
                                memo[(colors, cur_char)] = True
                    if (colors, cur_char) not in memo:
                        memo[(colors, cur_char)] = False
                elif cur_char == 'B':
                    for i in range(1, len(colors) - 1):
                        if colors[i - 1] == colors[i] == colors[i + 1] == 'B':
                            next_game = helper(colors[:i] + colors[i + 1:], 'A')
                            if next_game == False:
                                memo[(colors, cur_char)] = False
                    if (colors, cur_char) not in memo:
                        memo[(colors, cur_char)] = True
                return memo[(colors, cur_char)]
            return helper(colors, 'A')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    Another prev version of counting:

    class Solution:
        def winnerOfGame(self, colors: str) -> bool:
            cons_a, cons_b = 0, 0
            prev_ch = None
            cnt = 0
            for each_ch in colors:
                if prev_ch != each_ch:
                    if prev_ch:
                        if prev_ch == 'A':
                            cons_a += max(0, cnt - 2)
                        elif prev_ch == 'B':
                            cons_b += max(0, cnt - 2)
                    prev_ch = each_ch
                    cnt = 0
                cnt += 1
            if prev_ch == 'A':
                cons_a += max(0, cnt - 2)
            elif prev_ch == 'B':
                cons_b += max(0, cnt - 2)
            return cons_a > cons_b
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    CSS篇九
    .NET 高级开发人员面试常见问题及解答
    C/C++文件操作————写文件与读文件以及通讯录的改进 (保姆级教学)
    数学建模文写作----个人笔记
    iOS基础开发介绍
    Java全栈课程之SpringSecurity详解
    Java新手小白入门篇 API - 多线程
    Git学习笔记4
    线性规划在多种问题形式下的应用
    用于视频压缩伪影消除的深度卡尔曼滤波网络
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/133505791