Dota2 Senate

Leetcode Daily Challenge (4th May, 2023)

Dota2 Senate

Problem Statement:-

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".

Link: https://leetcode.com/problems/dota2-senate/description/

Problem Explanation with examples:-

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 <= 10<sup>4</sup>

  • senate[i] is either 'R' or 'D'.

Intuition:-

  • One thing is sure that we have to go from left to right.

  • To play optimally, each player will ban the next closest opponent.

  • Another thing is that we can go circularly. So, after one iteration, again the last player can ban the first opponent.

  • Keeping this in mind, we can use two queues to store the indices of the players. One queue for the Radiant players and the other for the Dire players.

  • We can iterate over the senate string and add the indices of the players to the respective queues. Then perform ban operations on the players optimally.

  • In the end, whichever queue is not empty will be the winner.

Solution:-

  • Initialize two queues, one for the Radiant players and the other for the Dire players.

  • Iterate over the senate string. If the current character is R, add the index to the Radiant queue. If the current character is D, add the index to the Dire queue.

  • While both the queues are not empty, pop the first element from both queues. If the index of the Radiant player is less than the index of the Dire player, add the index of the Radiant player to the Radiant queue by adding n to it. Else, add the index of the Dire player to the Dire queue by adding n to it.

  • Return the winner by checking which queue is not empty.

Code:-

JAVA Solution

class Solution {
    public String predictPartyVictory(String senate) {
        char[] senateArr = senate.toCharArray();
        int n = senateArr.length;
        Deque<Integer> r = new LinkedList<>();
        Deque<Integer> d = new LinkedList<>();

        for (int i = 0; i < n; i++) {
            if (senateArr[i] == 'R') {
                r.offer(i);
            } else {
                d.offer(i);
            }
        }

        while (!r.isEmpty() && !d.isEmpty()) {
            int dval = d.poll();
            int rval = r.poll();

            if (rval < dval) {
                r.offer(rval + n);
            } else {
                d.offer(dval + n);
            }
        }

        return r.isEmpty() ? "Dire" : "Radiant";
    }
}

Python Solution

class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        senate = list(senate)
        n = len(senate)
        r,d = deque(), deque()

        for i,c in enumerate(senate):
            if c == 'R':
                r.append(i)
            else:
                d.append(i)

        while r and d:
            dval = d.popleft()
            rval = r.popleft()

            if rval < dval:
                r.append(rval+n)
            else:
                d.append(dval+n)

        return "Radiant" if r else "Dire"

Complexity Analysis:-

TIME:-

The time complexity is O(n) where n is the length of the senate string. We iterate over the senate string once and perform ban operations on the players optimally.

SPACE:-

The space complexity is O(n) where n is the length of the senate string. We use two queues to store the indices of the players. In the worst case, all the players can be of the same type. In that case, we will have to store all the indices in the respective queues.

References:-

Connect with me:-

Did you find this article valuable?

Support Leeting-LCS by becoming a sponsor. Any amount is appreciated!