LeetCode題解(0649):Dota2參議院(Python)

長行發表於2021-01-04

題目:原題連結(中等)

標籤:貪心演算法、佇列

解法時間複雜度空間複雜度執行用時
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N)60ms (94.51%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        size = len(senate)

        queue1 = collections.deque()
        queue2 = collections.deque()
        for i, ch in enumerate(senate):
            if ch == "R":
                queue1.append(i)
            else:
                queue2.append(i)

        while queue1 and queue2:
            if queue1[0] < queue2[0]:
                queue1.append(queue1.popleft() + size)
                queue2.popleft()
            else:
                queue2.append(queue2.popleft() + size)
                queue1.popleft()

        return "Radiant" if queue1 else "Dire"

相關文章