Dota2 Senate (LeetCode 649): Solution and Explanation

Medium
String
Greedy
Queue

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

  1. Create a radiant queue and a dire queue and push every seat index into its party's queue in order.
  2. While both queues are non-empty, pop r from the Radiant queue and d from the Dire queue.
  3. If r < d, the Radiant senator acts first and bans the Dire senator. Push r + n back onto the Radiant queue.
  4. Otherwise the Dire senator acts first. Push d + n back onto the Dire queue.
  5. When one queue is empty, return the party whose queue still has senators.

Step-by-step example

senate = "RD"
  1. n = 2. The Radiant queue is [0] and the Dire queue is [1].
  2. Compare the fronts: 0 < 1, so Radiant seat 0 acts first and bans Dire seat 1.
  3. Seat 0 goes back as ticket 0 + 2 = 2 for the next round. The Radiant queue is [2] and the Dire queue is empty.
  4. 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"
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Two queues of turn indices O(n)
Single queue with floating bans O(n)
Round-by-round simulation with ban counters O(n²) worst case
Time

O(n)

Each loop iteration bans one senator, so there are at most n - 1 iterations, and each one does constant queue work.

Space

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 + n handles.
  • 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. Use collections.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.