Dota2 Senate — LeetCode 649 Python Solution
MediumGreedyQueueString
- Problem
- #649
- Pattern
- Stack
- Reading time
- 3 min
- Source
- leetcode.com
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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
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
On a study list
This problem is on LeetCode 75.
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.