1. 程式人生 > 其它 >【LeetCode】649. Dota2 Senate Dota2 參議院(Medium)(JAVA)每日一題

【LeetCode】649. Dota2 Senate Dota2 參議院(Medium)(JAVA)每日一題

技術標籤:LeetCode 每日一題leetcodejava演算法面試字串

【LeetCode】649. Dota2 Senate Dota2 參議院(Medium)(JAVA)

題目地址: https://leetcode.com/problems/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:

  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:

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

題目大意

Dota2 的世界裡有兩個陣營:Radiant(天輝)和 Dire(夜魘)

Dota2 參議院由來自兩派的參議員組成。現在參議院希望對一個 Dota2 遊戲裡的改變作出決定。他們以一個基於輪為過程的投票進行。在每一輪中,每一位參議員都可以行使兩項權利中的一項:

  1. 禁止一名參議員的權利:參議員可以讓另一位參議員在這一輪和隨後的幾輪中喪失所有的權利。

  2. 宣佈勝利:如果參議員發現有權利投票的參議員都是同一個陣營的,他可以宣佈勝利並決定在遊戲中的有關變化。

給定一個字串代表每個參議員的陣營。字母 “R” 和 “D” 分別代表了 Radiant(天輝)和 Dire(夜魘)。然後,如果有 n 個參議員,給定字串的大小將是 n。

以輪為基礎的過程從給定順序的第一個參議員開始到最後一個參議員結束。這一過程將持續到投票結束。所有失去權利的參議員將在過程中被跳過。

假設每一位參議員都足夠聰明,會為自己的政黨做出最好的策略,你需要預測哪一方最終會宣佈勝利並在 Dota2 遊戲中決定改變。輸出應該是 Radiant 或 Dire。

解題方法

  1. 這道題主要在題目的理解上,題目的大意就是,每個位置上的參議員都可以指定一個位置上的參議員在接下來失去權力,最後留下來都是一派的參議員,就勝利了
  2. 這樣參議員肯定投的另一派,而且肯定會優先讓沒有投過的參議員失去權力
  3. 所以就把每一派的位置用兩個佇列 queue 存起來,然後不斷取出,在前面的肯定是會把在後面的投出去,剩下的就等下一輪再投,所以要在當前位置 + len 繼續放到佇列裡
  4. 直到一個佇列裡沒有元素,說明這一對輸了,另一對贏了
class Solution {
    public String predictPartyVictory(String senate) {
        Queue<Integer> rad = new LinkedList<>();
        Queue<Integer> dire = new LinkedList<>();
        int len = senate.length();
        for (int i = 0; i < senate.length(); i++) {
            if (senate.charAt(i) == 'R') {
                rad.offer(i);
            } else {
                dire.offer(i);
            }
        }
        while (!rad.isEmpty() && !dire.isEmpty()) {
            int radIndex = rad.poll();
            int direIndex = dire.poll();
            if (radIndex < direIndex) {
                rad.offer(radIndex + len);
            } else {
                dire.offer(direIndex + len);
            }
        }
        return rad.isEmpty() ? "Dire" : "Radiant";
    }
}

執行耗時:13 ms,擊敗了36.43% 的Java使用者
記憶體消耗:39.4 MB,擊敗了25.00% 的Java使用者

歡迎關注我的公眾號,LeetCode 每日一題更新