LeetCode 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 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:this

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

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

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

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:ip

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:ci

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

 題意概述:字符串

DOTA2 參議院由Dire和Radiant兩派的參議員組成,每一輪的投票中,兩邊的參議員都有權利使另外一派的參議員喪失投票權利或者當全部參議員只剩下本身這一派時宣佈勝利,而後讓咱們求對於輸入的一個由R、D組成字符串,其從左至右表明兩派參議員的位置順序,讓咱們在假設兩派參議員都作出最優決策的狀況下判斷哪一派會勝出。string

算法分析:

這題很明顯是貪心算法,輪到任意一派的參議員最優的決策爲讓最靠近本身的另外一派參議院喪失其接下來的投票權利。所以能夠創建兩個隊列來作,隊列的做用就是記錄各自門派中參議員的投票次序,位於隊首的參議員具備優先投票權且必定是與另外一派隊首參議員最近。兩個隊首元素進行比較,位置小的可使位置大的喪失投票權,而後從新加入到隊列中。因爲須要循環進行投票,所以在隊列中記錄的參議員的次序須要不斷的發生變化來表示當前的投票次序即所處的位置。

代碼:

class Solution {
    public String predictPartyVictory(String senate) {
        Queue<Integer> que1 = new LinkedList<Integer>();
        Queue<Integer> que2 = new LinkedList<Integer>();
        int n=senate.length();
        for(int i=0;i<n;i++){
            if(senate.charAt(i)=='R'){
                que1.offer(i);
            }
            else{
                que2.offer(i);
            }
        }
        while(que1.peek()!=null&&que2.peek()!=null){
            int a = que1.poll();
            int b = que2.poll();
            if(a<b)
                que1.offer(a+n);
            else
                que2.offer(b+n);
        }
        return que1.peek()==null?"Dire":"Radiant";
    }
}
相關文章
相關標籤/搜索