Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/649.html

649. Dota2 Senate

Level

Medium

Description

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 make a decision about 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:

  1. Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
  2. 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 make the decision about the change in the game.

Given a string representing each senator’s party belonging. The character ‘R’ and ‘D’ represent the Radiant party and the Dire party respectively. 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, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.

Example 1:

Input: “RD”

Output: “Radiant”

Explanation: The first senator comes from Radiant and he can just ban the next senator’s right in the round 1.

And the second senator can’t exercise any rights any more since his right has been banned.

And in the 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: “RDD”

Output: “Dire”

Explanation: The first senator comes from Radiant and he can just ban the next senator’s right in the 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 the round 1.

And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.

Note:

  1. The length of the given string will in the range [1, 10,000].

Solution

The best strategy is to ban the next senator of the other party. Use a queue to store the parties of the senators. Initially, offer all parties from senate to the queue in the original order, and count the number of senators in Radiant and the number of senators in Dire. Initialize the ban counts of both parties to 0.

While both parties have remaining senators, each time poll an element from the queue and check the party. Check whether the ban count of the party is greater than 0. If so, then the current senator is banned and does not have any rights, so decrease the ban count of the party by 1. Otherwise, the senator can ban the next senator of the other party, so decrease the number of senators in the other party by 1, increase the ban count of the other party by 1, and offer the element to the queue again.

If one party’s number of senators is decreased to 0, then break the loop, and this party loses. The other party will finally announce the victory.

  • public class Solution {
        public String predictPartyVictory(String senate) {
            Queue<Character> queue = new LinkedList<Character>();
            int radiantCount = 0, direCount = 0;
            int length = senate.length();
            for (int i = 0; i < length; i++) {
                char party = senate.charAt(i);
                queue.offer(party);
                if (party == 'R')
                    radiantCount++;
                else
                    direCount++;
            }
            int radiantBanCount = 0, direBanCount = 0;
            while (radiantCount > 0 && direCount > 0) {
                char party = queue.poll();
                if (party == 'R') {
                    if (radiantBanCount > 0)
                        radiantBanCount--;
                    else {
                        direCount--;
                        direBanCount++;
                        queue.offer(party);
                    }
                } else {
                    if (direBanCount > 0)
                        direBanCount--;
                    else {
                        radiantCount--;
                        radiantBanCount++;
                        queue.offer(party);
                    }
                }
            }
            return radiantCount > 0 ? "Radiant" : "Dire";
        }
    }
    
  • // OJ: https://leetcode.com/problems/dota2-senate/
    // Time: O(N)
    // Space: O(N)
    class Solution {
    public:
        string predictPartyVictory(string s) {
            queue<int> q;
            int cnt[2] = {}, ban[2] = {};
            for (char c : s) {
                int x = c == 'R' ? 1 : 0;
                cnt[x]++;
                q.push(x);
            }
            while (cnt[0] && cnt[1]) {
                int x = q.front();
                q.pop();
                if (ban[x]) {
                    ban[x]--;
                    cnt[x]--;
                } else {
                    ban[1 - x]++;
                    q.push(x);
                }
            }
            return cnt[1] ? "Radiant" : "Dire";
        }
    };
    
  • class Solution(object):
        def predictPartyVictory(self, senate):
            """
            :type senate: str
            :rtype: str
            """
            q_r, q_d = collections.deque(), collections.deque()
            n = len(senate)
            for i, s in enumerate(senate):
                if s == "R":
                    q_r.append(i)
                else:
                    q_d.append(i)
            while q_r and q_d:
                r = q_r.popleft()
                d = q_d.popleft()
                if r < d:
                    q_r.append(r + n)
                else:
                    q_d.append(d + n)
            return "Radiant" if q_r else "Dire"
    

All Problems

All Solutions