SDE & MLE Prep

649. Dota2 Senate

Problem

Senators from two parties, 'R' (Radiant) and 'D' (Dire), act in the order of the string, round after round. On their turn, a senator still in play either removes one opposing senator's rights for good or, if only their own party is left, declares victory. Assuming everyone plays optimally, return "Radiant" or "Dire".

Example: "RD" → "Radiant"; "RDD" → "Dire"

Constraints: length ≤ 10⁴.

Two queues of senator indices. The earlier senator bans the other; the winner re-enters with index + n (next round).

Complexity: O(n).

class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        n = len(senate)
        r = deque(i for i, c in enumerate(senate) if c == "R")
        d = deque(i for i, c in enumerate(senate) if c == "D")
        while r and d:
            a, b = r.popleft(), d.popleft()
            if a < b:
                r.append(a + n)
            else:
                d.append(b + n)
        return "Radiant" if r else "Dire"