MediumNo. 0649
Dota2 Senate — LeetCode 649 Python Solution
- Pattern
- Stack
- Topics
- Greedy, Queue, String
- On a study list
- LeetCode 75
- 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
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
| No. | Problem | Difficulty |
|---|---|---|
| 0936 | Stamping The Sequence | Hard |
| 0179 | Largest Number | Medium |
| 0316 | Remove Duplicate Letters | Medium |
| 0402 | Remove K Digits | Medium |
| 0420 | Strong Password Checker | Hard |
| 0680 | Valid Palindrome II | Easy |
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.