{x}
blog image

Dota2 Senate

In the world of Dota2, there are two parties: the Radiant and the Dire.

The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:

  • Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
  • Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.

Given a string senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.

The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.

Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire".

 

Example 1:

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. 
And the second senator can't exercise any rights anymore since his right has been banned. 
And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.

Example 2:

Input: senate = "RDD"
Output: "Dire"
Explanation: 
The first senator comes from Radiant and he can just ban the next senator's right in round 1. 
And the second senator can't exercise any rights anymore since his right has been banned. 
And the third senator comes from Dire and he can ban the first senator's right in round 1. 
And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.

 

Constraints:

  • n == senate.length
  • 1 <= n <= 104
  • senate[i] is either 'R' or 'D'.

649. Dota2 Senate

Problem Description

The problem simulates a voting process in a senate with two parties, Radiant (R) and Dire (D). Senators take turns banning opponents or declaring victory if their party holds a majority. The goal is to predict the winning party.

Solution: Queue-based Simulation

The optimal approach uses two queues, one for Radiant (qr) and one for Dire (qd), storing the indices of senators from each party. The simulation proceeds round by round:

  1. Initialization: Populate the queues with senator indices based on the input string senate.

  2. Round Simulation: While both queues are not empty:

    • Dequeue the first senator from each queue.
    • Compare their indices. The senator with the smaller index bans the other.
    • The banned senator's index is effectively removed (not explicitly removed but ignored in future rounds). The banning senator's index is added to the end of their queue (using n to represent the fact that this senator will participate in the next round).
  3. Victory Condition: The simulation ends when one queue is empty. The remaining queue's party wins.

Time and Space Complexity

  • Time Complexity: O(n), where n is the number of senators. In the worst case, each senator is banned exactly once. Each pop and push on the queue is O(1), so the simulation takes linear time.

  • Space Complexity: O(n). In the worst case, all senators are in one queue, using linear space.

Code Implementation (Python)

from collections import deque
 
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)  # Radiant bans Dire
            else:
                qd.append(qd[0] + n)  # Dire bans Radiant
            qr.popleft()
            qd.popleft()
        return "Radiant" if qr else "Dire"
 

The code in other languages (Java, C++, Go, TypeScript, Rust) follows a very similar structure, utilizing queues (deque in Python, ArrayDeque in Java, queue in C++, slices in Go, arrays in TypeScript, VecDeque in Rust) to efficiently manage the senators. The core logic of comparing indices and adding back to the queue remains consistent across all languages.