MediumNo. 0649

Dota2 Senate — LeetCode 649 Python Solution

Pattern
Stack
Topics
Greedy, Queue, String
On a study list
LeetCode 75
Reading time
3 min

The problem

In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties.

Example

Input
senate = "RD"
Output
"Radiant"
Explanation
The first senator comes from Radiant and he can just ban the next senator's right in round 1.

Python solution

PythonSolution
class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        qr = deque()
        qd = deque()
        for i, c in enumerate(senate):
            if c == "R":
                qr.append(i)
            else:
                qd.append(i)
        n = len(senate)
        while qr and qd:
            if qr[0] < qd[0]:
                qr.append(qr[0] + n)
            else:
                qd.append(qd[0] + n)
            qr.popleft()
            qd.popleft()
        return "Radiant" if qr else "Dire"

Complexity

Time
O(n)
Space · auxiliary
O(n)

Pattern: Stack

When the most recent unresolved thing is the one that matters, use a stack. LeetCode 649. Dota2 Senate is filed here because LeetCode tags it Queue, which is the vocabulary this hub collects.

The stack guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0936Stamping The SequenceHard
0179Largest NumberMedium
0316Remove Duplicate LettersMedium
0402Remove K DigitsMedium
0420Strong Password CheckerHard
0680Valid Palindrome IIEasy

Frequently asked questions

How hard is LeetCode 649. Dota2 Senate?

LeetCode 649. Dota2 Senate is rated Medium on LeetCode.

What is the time complexity of LeetCode 649. Dota2 Senate?

The Python solution on this page runs in O(n).

What is the space complexity of LeetCode 649. Dota2 Senate?

The Python solution on this page uses O(n) auxiliary space.

What topics does LeetCode 649. Dota2 Senate cover?

LeetCode 649. Dota2 Senate is tagged Greedy, Queue and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.