Dota2 Senate

Senators of two parties take turns banning each other; find out which party ends up holding every vote.

A senate has senators from two parties, Radiant and Dire. The string senate lists them in the order they vote: 'R' for a Radiant senator and 'D' for a Dire one. Voting goes in rounds, from the first senator to the last, and then from the first again.

On their turn, a senator who has not been banned may ban one senator of the other party. A banned senator loses all of their future turns. As soon as everyone who can still vote belongs to one party, that party wins. Every senator plays as well as possible for their own party. Return "Radiant" or "Dire".

Example 1
Input: senate = "RD"
Output: "Radiant"
Explanation: R votes first and bans D.
Example 2
Input: senate = "RDD"
Output: "Dire"
Explanation: R bans one D, and the other D then bans R.
Example 3
Input: senate = "RDRDD"
Output: "Radiant"
Explanation: Dire has more senators, but the two R senators vote early enough to win.
Solution

The Idea

Picture a group of players sitting in a row and taking turns, where a turn lets you send one player of the other team out of the game. Two things decide who wins: how many players each team has, and when they get their turns.

Example 2 shows the number of players at work. In "RDD", the R votes first and bans one D. The other D still has a turn, and uses it to ban the R. Dire wins because it had one senator to spare.

Example 3 shows that the turns matter just as much. In "RDRDD", Dire has three senators and Radiant only two, yet Radiant wins. This raises the real question: when it is your turn, which opponent should you ban?

Number the senators by their place in the string, starting from 0, and call that number their position. The R at position 0 has three choices: the D at position 1, 3 or 4. Suppose it bans the D at position 3. Then the D at position 1 votes next and bans the R at position 2. The D at position 4 bans the R at position 0, and Dire wins.

The D at position 1 was the danger, because it was the next opponent to vote. So the best ban is always the next opponent in voting order, counting on from the end back to the start. That opponent would act against you soonest, and any later opponent can still be banned later.

The senate RDRDD over two rounds. In round 1 the R at position 0 bans the D at position 1, the R at position 2 bans the D at position 3, and the D at position 4 bans the R at position 0. In round 2 the R at position 2 bans the D at position 4, and only Radiant is left.

Look at the picture to see every senator follow that rule. In round 1, the R at position 0 bans the D at position 1 (arrow 1), and the R at position 2 bans the D at position 3 (arrow 2). The D at position 4 has no opponent after it, so it counts on from the start and bans the R at position 0 (arrow 3).

In round 2, the banned senators are greyed out and skip their turns. The R at position 2 votes first and bans the D at position 4 (arrow 4). Only Radiant senators can still vote, so Radiant wins.

Solution Approach

Turning that rule into code means settling three things: how to find the next opponent quickly, how a senator gets their turn in the next round, and when the voting ends.

Simulating the rounds directly

What is the simplest way to play the game? Walk through the string again and again. For each senator who is not banned, scan forward, wrapping to the start, until you reach an opponent who is not banned, and ban them.

This works, but a scan can pass over many banned senators before it finds an opponent, up to n of them, where n is the number of senators. With up to n bans, that adds up to about n × n steps.

Two queues of positions

Can we find the next opponent without scanning? Keep each party's senators in a queue, a list where you add at the back and take from the front. The Radiant queue holds the positions of the R senators in voting order, and the Dire queue those of the D senators.

Now compare the two fronts. The smaller position votes first, and the front of the other queue is exactly its next opponent. One comparison decides one ban: the loser is taken out and never put back.

Coming back for the next round

Where does the senator who just voted go? They vote again in the next round, after everyone still waiting in this round. Adding n to their position puts them exactly there.

In "RDRDD", n is 5, so the R at position 0 comes back as 5. That is larger than every position left in round 1, which go up to 4. It is also smaller than the R at position 2, which comes back as 7, so the order of round 1 carries over to round 2.

When the voting ends

Each comparison removes one senator for good. When one queue is empty, only the other party can still vote, and it wins.

Java Solution

The method predictPartyVictory works in these steps:

  1. First, build the two queues, radiant and dire. One pass over the string adds each senator's position to their party's queue, so each queue holds its positions in voting order.
  2. While both queues still have senators, take the front of each: r from radiant and d from dire.
  3. The smaller position votes first and bans the other. The winner goes back into its queue with position r + n or d + n, to vote again in the next round. The banned senator is simply not put back.
  4. When one queue is empty, the party left in the other queue wins, so return its name.

The main method tries the three examples, their mirror image "DDRRR", a senate of a single senator, and a senate where the smaller party votes first.

Java
import java.util.ArrayDeque;
import java.util.Queue;

public class Dota2Senate {

    public String predictPartyVictory(String senate) {
        int n = senate.length();
        Queue<Integer> radiant = new ArrayDeque<>();
        Queue<Integer> dire = new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            if (senate.charAt(i) == 'R') {
                radiant.add(i);
            } else {
                dire.add(i);
            }
        }
        while (!radiant.isEmpty() && !dire.isEmpty()) {
            int r = radiant.poll();
            int d = dire.poll();
            if (r < d) {
                radiant.add(r + n);   // r bans d, and votes again next round
            } else {
                dire.add(d + n);      // d bans r, and votes again next round
            }
        }
        return radiant.isEmpty() ? "Dire" : "Radiant";
    }

    public static void main(String[] args) {
        Dota2Senate senate = new Dota2Senate();
        String[] tests = {"RD", "RDD", "RDRDD", "DDRRR", "R", "RRRRDDDDD"};
        for (String s : tests) {
            System.out.println(s + " -> " + senate.predictPartyVictory(s));
        }
    }
}

Running it prints:

Output
RD -> Radiant
RDD -> Dire
RDRDD -> Radiant
DDRRR -> Dire
R -> Radiant
RRRRDDDDD -> Radiant

The first three lines match the examples. Look at the third: RDRDD gives Radiant, even though Dire has three senators to Radiant's two. The fourth line swaps the parties, and DDRRR gives Dire, as it should.

The single R gives Radiant without any voting at all. The Dire queue starts empty, so the loop never runs. The last line has four R senators against five D senators, and Radiant still wins. The four R senators ban the first four D senators, the last D bans one R, and the next R bans that last D.

The + n is easy to drop, but the senator who just voted must not keep their old position. If they did, the R at position 0 in "RDD" would stay at the front with the smallest position and ban both D senators. Running the method that way returns Radiant for "RDD", but the D at position 2 really votes before the R's next turn, so Dire should win.

Complexity

Every pass of the loop bans one senator for good, so the work grows in step with the size of the senate.

  • Time: O(n), where n is the number of senators. Filling the queues reads each senator once, and the loop runs at most n - 1 times, because each pass takes two senators out and puts only one back.
  • Space: O(n). Together the two queues hold every senator still in the game, at most n positions.

Queues that send items to the back for another round also appear in Task Scheduler.