<AI/>
enru
All posts

Dota2 senate

LeetCode 649: Predict which party wins a round-based senate vote by simulating it with a queue.

Difficulty
Medium
Published
Topics
#string#queue
Open on LeetCode

Problem Description

In the world of Dota2 there are two parties: Radiant and Dire. To make a change to the game, the senate holds a vote, and each senator belongs to one of the two parties.

The voting goes in rounds, and in each round every senator is asked in turn. A senator can do one of two things:

  • ban the next senator of the opposite party from voting;
  • announce victory for their party if there are no senators with voting rights left in the opposite party.

A round starts with the leftmost senator.

Write a function that computes the outcome of the vote.

Input

A string describing the order of the voting senators and the parties they belong to.

Output

A string with the name of the winning party (Radiant or Dire).

Constraints

  • The number of senators (input string length) is in the range from 1 to 10000
  • The input string consists of the characters R and D

Examples

Input: RD

Answer: Radiant

Explanation: In the first round, the first senator from Radiant bans the senator from Dire. The round ends here, since there are no more senators with voting rights. In the second round, the Radiant senator announces victory for their party.

Solution

At first glance, it seems the problem can be solved by simply comparing the number of senators in each party. In fact, that would be wrong, since it ignores the voting order.

Let's look at a simple example: RDRDRDRDRDDD. Here there are 5 senators from Radiant and 7 from Dire. Let's walk through it step by step (the _ character marks senators who have lost their voting rights).

  • Obviously, the winning strategy for each Radiant senator is to ban the next Dire senator after them. So, given the voting order, the first 5 senators to vote in the first round are all from Radiant. After they vote, the picture looks like this: R_R_R_R_R_DD.
  • So only 2 Dire senators get to vote in the first round. The final outcome of the first round is: ____R_R_R_DD
  • In the second round, the Radiant senators vote first again. The first two ban the remaining two Dire senators, and the third one announces victory for their party. The final outcome of the second round is: ____R_R_R___.

So despite Dire having more senators, Radiant wins.

A queue, one of the basic data structures, helps us solve this problem. We'll use an array as the queue. We can forget that the vote happens in rounds and think of the whole process as one continuous loop.

First, we put all senators into the queue in their order and count the total number of members of each party.

Then, while the queue contains members of both parties, we run our continuous loop.

  • Store the party of the senator at the head of the queue in firstElem.
  • Introduce an extra counter killCount. It accumulates the number of senators that members of the firstElem party can ban before the right to vote passes to the opposite party.
  • Start taking senators out of the queue:
    • If the senator belongs to the same party as firstElem, increment killCount and put the senator at the end of the queue.
    • If the senator belongs to the opposite party, decrement killCount and skip the senator (do NOT put them back into the queue).
    • If killCount becomes 0, the firstElem senators can no longer ban the next senator, so firstElem is set to the party of the next senator in the queue and the count starts over, as long as the queue contains members of both parties.
typescript
const moveFirst = (arr: string[]) => {
  const first = arr.shift();
 
  if (first) {
    arr.push(first);
  }
};
 
export const predictPartyVictory = (senate: string): string => {
  let rCount = 0;
  let dCount = 0;
 
  const queue: string[] = [];
 
  for (let i = 0; i < senate.length; i++) {
    if (senate[i] === 'R') {
      rCount++;
    } else {
      dCount++;
    }
 
    queue.push(senate[i]);
  }
 
  while (rCount > 0 && dCount > 0) {
    let firstElem = queue[0];
    let killCount = 1;
 
    moveFirst(queue);
 
    while (killCount > 0 && rCount > 0 && dCount > 0) {
      if (queue[0] == firstElem) {
        moveFirst(queue);
        killCount++;
      } else {
        if (queue[0] == 'R') {
          rCount--;
        } else {
          dCount--;
        }
        killCount--;
        queue.shift();
      }
    }
  }
 
  if (rCount > 0) {
    return 'Radiant';
  }
 
  return 'Dire';
};

Complexity Analysis

Time Complexity

O(n), where n is the length of the string.

Space Complexity

O(n), since we create a queue to store the data.