[Swift]LeetCode649. Dota2 参议院 | Dota2 Senate
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/10485423.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
In the world of Dota2, there are two parties: the?Radiantand 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:
A senator can make another senator lose?all his rights?in this and all the following rounds.
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:
?Dota2 的世界里有兩個陣營:Radiant(天輝)和?Dire(夜魘)
Dota2 參議院由來自兩派的參議員組成。現在參議院希望對一個 Dota2 游戲里的改變作出決定。他們以一個基于輪為過程的投票進行。在每一輪中,每一位參議員都可以行使兩項權利中的一項:
禁止一名參議員的權利:
參議員可以讓另一位參議員在這一輪和隨后的幾輪中喪失所有的權利。
宣布勝利:如果參議員發現有權利投票的參議員都是同一個陣營的,他可以宣布勝利并決定在游戲中的有關變化。
給定一個字符串代表每個參議員的陣營。字母“R”和“D”分別代表了Radiant(天輝)和?Dire(夜魘)。然后,如果有 n 個參議員,給定字符串的大小將是n。
以輪為基礎的過程從給定順序的第一個參議員開始到最后一個參議員結束。這一過程將持續到投票結束。所有失去權利的參議員將在過程中被跳過。
假設每一位參議員都足夠聰明,會為自己的政黨做出最好的策略,你需要預測哪一方最終會宣布勝利并在 Dota2 游戲中決定改變。輸出應該是?Radiant?或?Dire。
示例 1:
輸入: "RD" 輸出: "Radiant" 解釋: 第一個參議員來自 Radiant 陣營并且他可以使用第一項權利讓第二個參議員失去權力,因此第二個參議員將被跳過因為他沒有任何權利。然后在第二輪的時候,第一個參議員可以宣布勝利,因為他是唯一一個有投票權的人示例 2:
輸入: "RDD" 輸出: "Dire" 解釋: 第一輪中,第一個來自 Radiant 陣營的參議員可以使用第一項權利禁止第二個參議員的權利 第二個來自 Dire 陣營的參議員會被跳過因為他的權利被禁止 第三個來自 Dire 陣營的參議員可以使用他的第一項權利禁止第一個參議員的權利 因此在第二輪只剩下第三個參議員擁有投票的權利,于是他可以宣布勝利?注意:
Runtime:?144 ms Memory Usage:?19.7 MB 1 class Solution { 2 func predictPartyVictory(_ senate: String) -> String { 3 var arr:[Character] = Array(senate) 4 var n:Int = senate.count 5 var q1:[Int] = [Int]() 6 var q2:[Int] = [Int]() 7 for i in 0..<n 8 { 9 if arr[i] == "R" 10 { 11 q1.append(i) 12 } 13 else 14 { 15 q2.append(i) 16 } 17 } 18 while(!q1.isEmpty && !q2.isEmpty) 19 { 20 var i:Int = q1.removeFirst() 21 var j:Int = q2.removeFirst() 22 if i < j 23 { 24 q1.append(i + n) 25 } 26 else 27 { 28 q2.append(j + n) 29 } 30 } 31 return (q1.count > q2.count) ? "Radiant" : "Dire" 32 } 33 }
?
轉載于:https://www.cnblogs.com/strengthen/p/10485423.html
總結
以上是生活随笔為你收集整理的[Swift]LeetCode649. Dota2 参议院 | Dota2 Senate的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 如何将一个数组对象 把对象的值用指定符号
- 下一篇: Java面试题之类的静态代码块和静态属性
