Skip to content

Latest commit

 

History

History
 
 

649. Dota2 Senate

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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

 

Related Topics:
Greedy

Similar Questions:

Solution 1.

For a senator, he will try to first ban a rival after him. If there is no rival after him, then he ban the first rival in the queue.

This can be simplified if we think the queue in a circular manner -- the senator in the next around will be appended to the queue in this round.

Another trick is that, a senator doesn't need to perform the ban right away; he can increment the number of bans to his rival and then jump to the end of the queue. So when a senate pops from the queue, he needs to first check if there is any ban left for him. If yes, take the ban and leave. Otherwise, increment the bans to his rival and jump to the end of the queue.

// OJ: https://leetcode.com/problems/dota2-senate/
// Author: github.com/lzl124631x
// 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";
    }
};