2023年10月2日 星期一

10/1 每日一題(輪流刪除各自的顏色,只有前後相同才可刪除,當無法刪除時,比賽結束)

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'

 

class Solution:
    def winnerOfGame(self, colors: str) -> bool:
        # n=0
        Alice=0
        Bob=0
        # while n < len(colors):
        #     if colors[n:n+3]=='AAA':
        #         Alice +=1
           
        #     elif colors[n:n+3] =='BBB':
        #         Bob += 1
           
           
        #     n += 1
       
        # return Alice > Bob
       
        for i in range(len(colors)):
            if colors[i:i+3]=="AAA":
                Alice+=1
            elif colors[i:i+3]=="BBB":
                Bob+=1
        return Alice > Bob

-----------------------------參考
class Solution:
    def winnerOfGame(self, colors: str) -> bool:
        a_counter = 0
        b_counter = 0
        last_len = len(colors)
        all_colors = 'AB'
        for color in all_colors:
            while color * 3 in colors:
                colors = colors.replace(color * 3, color * 2)
                if color == 'A':
                    a_counter += (last_len - len(colors))
                    last_len = len(colors)
                else:
                    b_counter += (last_len - len(colors))
                    last_len = len(colors)
        
return a_counter > b_counter           

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁