649. Dota2 Senate

649. 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 <= 10^4

senate[i] is either ‘R’ or ‘D’.


문제 해석

  • “Radiant” 와 “Dire” 두 정당소속 사람들이 존재한다.
  • 사람들은 라운드마다 반대파 정당 투표권을 없애거나, 승리선언 둘 중 하나의 행동만 취할 수 있다.
  • 승리선언은 한 정당만 남고 행동 기회가 남아있을 때 취할 수 있다.
  • 순서가 앞에 있는 사람의 행동이 절대적으로 우선시 된다.

구현

class Solution {
public:
    string predictPartyVictory(string senate) {
        queue<int> rad;
        queue<int> dir;
        int n = senate.length();

        for(int i = 0; i < n; ++i)
        {
            if(senate[i] == 'R')
            {
                rad.push(i);
            }
            else
            {
                dir.push(i);
            }
        }

        while(!rad.empty() && !dir.empty())
        {
            if(rad.front() < dir.front())
            {
                rad.pop();
                dir.pop();
                rad.push(n++);
            }
            else
            {
                rad.pop();
                dir.pop();
                dir.push(n++);
            }
        }

        if(!rad.empty())
        {
            return "Radiant";
        }
        else
        {
            return "Dire";
        }
    }
};

코드 해석

for(int i = 0; i < n; ++i)
        {
            if(senate[i] == 'R')
            {
                rad.push(i);
            }
            else
            {
                dir.push(i);
            }
        }
  • 이번 문제에서 핵심코드
  • rad와 dir 두 정당의 queue 를 각각 생성한다.
  • 그리고 각 큐에는 정당 사람의 인덱스 번호 를 저장시켜준다(인덱스 번호가 작을 수록 우선투표권이라는 뜻, 투표순서가 중요한 문제이기 때문에 이렇게 한 것이다.)
while(!rad.empty() && !dir.empty())
        {
            if(rad.front() < dir.front())
            {
                rad.pop();
                dir.pop();
                rad.push(n++);
            }
            else
            {
                rad.pop();
                dir.pop();
                dir.push(n++);
            }
        }
  • 큐 rad 와 dir 에는 투표자의 순서 인덱스가 들어가 있기 때문에 비교문을 통해 만약 먼저 투표를 한다면 다른 정당의 투표자를 없애고 자기 자신은 또 투표할 수 있기 때문에 다시 push()를 통해 맨 뒤 순서(senate.length())에 다시 넣어준다.