Dota2 Senate (LeetCode 649): Solution and Explanation
Senators from two parties, Radiant (R) and Dire (D), act in seat order, round after round. On a turn a senator can ban any other senator for good, or declare victory once only their own party can still vote. Everyone plays optimally. Return the party that wins.
Intuition
Think about what a senator should do on their turn. Declaring victory early is impossible while opponents can still vote, so the real choice is whom to ban. The dangerous opponent is the one who will act next, because that person is about to ban someone from your party. Banning an opponent who acts later is never better: the one who acts sooner would still get their turn and do damage.
So the whole game becomes a race in turn order. Write each senator as the index at which they next act. Compare the next Radiant turn with the next Dire turn. Whoever comes first bans the other and then waits for their next turn, which is one full round later.
That maps cleanly onto two queues of indices. Pop the two fronts, the smaller index wins, and the winner goes to the back of its own queue with index + n. Adding n keeps the order right: the winner acts again only after everyone else who still has a turn in this round.
Every comparison removes exactly one senator, so the loop runs at most n - 1 times. The party whose queue is still non-empty at the end is the winner.
Approach: Two queues of turn indices
- Create a
radiantqueue and adirequeue and push every seat index into its party's queue in order. - While both queues are non-empty, pop
rfrom the Radiant queue anddfrom the Dire queue. - If
r < d, the Radiant senator acts first and bans the Dire senator. Pushr + nback onto the Radiant queue. - Otherwise the Dire senator acts first. Push
d + nback onto the Dire queue. - When one queue is empty, return the party whose queue still has senators.
Step-by-step example
senate = "RD"
- n = 2. The Radiant queue is [0] and the Dire queue is [1].
- Compare the fronts: 0 < 1, so Radiant seat 0 acts first and bans Dire seat 1.
- Seat 0 goes back as ticket 0 + 2 = 2 for the next round. The Radiant queue is [2] and the Dire queue is empty.
- The loop stops because the Dire queue is empty, so the function returns "Radiant".
Code: Python, Java, C++
1class Solution:
2 def predictPartyVictory(self, senate: str) -> str:
3 n = len(senate)
4 radiant = deque()
5 dire = deque()
6 for i, c in enumerate(senate):
7 if c == 'R':
8 radiant.append(i)
9 else:
10 dire.append(i)
11 while radiant and dire:
12 r = radiant.popleft()
13 d = dire.popleft()
14 if r < d:
15 radiant.append(r + n)
16 else:
17 dire.append(d + n)
18 return "Radiant" if radiant else "Dire"1class Solution {
2 public String predictPartyVictory(String senate) {
3 int n = senate.length();
4 Deque<Integer> radiant = new ArrayDeque<>();
5 Deque<Integer> dire = new ArrayDeque<>();
6 for (int i = 0; i < n; i++) {
7 if (senate.charAt(i) == 'R') {
8 radiant.offer(i);
9 } else {
10 dire.offer(i);
11 }
12 }
13 while (!radiant.isEmpty() && !dire.isEmpty()) {
14 int r = radiant.poll();
15 int d = dire.poll();
16 if (r < d) {
17 radiant.offer(r + n);
18 } else {
19 dire.offer(d + n);
20 }
21 }
22 return radiant.isEmpty() ? "Dire" : "Radiant";
23 }
24}1class Solution {
2public:
3 string predictPartyVictory(string senate) {
4 int n = senate.size();
5 queue<int> radiant;
6 queue<int> dire;
7 for (int i = 0; i < n; i++) {
8 if (senate[i] == 'R') {
9 radiant.push(i);
10 } else {
11 dire.push(i);
12 }
13 }
14 while (!radiant.empty() && !dire.empty()) {
15 int r = radiant.front(); radiant.pop();
16 int d = dire.front(); dire.pop();
17 if (r < d) {
18 radiant.push(r + n);
19 } else {
20 dire.push(d + n);
21 }
22 }
23 return radiant.empty() ? "Dire" : "Radiant";
24 }
25};Time and space complexity
O(n)
Each loop iteration bans one senator, so there are at most n - 1 iterations, and each one does constant queue work.
O(n)
The two queues together hold one entry per senator who can still vote.
Other approaches
Round-by-round simulation with ban counters (time O(n²) worst case, space O(n))
Walk the string repeatedly, keeping a count of pending bans for each party. When a senator is reached and their party has pending bans, they are removed; otherwise they add a pending ban against the other party. It gives the same answer but loops over the string many times.
Single queue with floating bans (time O(n), space O(n))
Keep one queue of characters plus two counters of bans waiting to be applied. Pop a senator, skip it if a ban against its party is pending, otherwise record a ban against the other party and push it back. It is also linear and uses the same greedy rule.
Common mistakes
- Banning the first opponent in the string instead of the next opponent after the current senator. Order matters across the round boundary, which is what
index + nhandles. - Re-inserting the winner with its old index. It must act after everyone left in the current round, so add n.
- Counting party sizes and returning the bigger party. Seat order decides the game, so "DDRRR" is won by Dire even though Radiant has more senators.
- Using a list with
pop(0)in Python, which makes each pop linear. Usecollections.deque.
Frequently asked questions
Why is banning the next opposing senator the best move?
That senator is the next one who can hurt your party. Any opponent further away will act later, so if you leave the next one alive, they ban one of yours first. Banning them is never worse than any other choice.
Why add n when the winner goes back into the queue?
The winner has used their turn for this round. Adding n places their next turn after every remaining turn of the current round while keeping their relative order with other returning senators.
Can the party with more senators lose?
Yes. In "DDRRR" the two Dire senators act first and ban two Radiant senators, then the next round lets Dire ban the last one. Seat order matters as much as headcount.
What is the time complexity of the Dota2 Senate queue solution?
It is O(n) time and O(n) space. Every comparison bans one senator, so there are fewer than n comparisons.