★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-hewtwzez-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
In the world of Dota2, there are two parties: the Radiant
and the Dire
.git
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:github
Ban one senator's right
:Announce the victory
: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.app
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
.this
Example 1:spa
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:code
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:htm
Dota2 的世界裏有兩個陣營:Radiant
(天輝)和 Dire
(夜魘)blog
Dota2 參議院由來自兩派的參議員組成。如今參議院但願對一個 Dota2 遊戲裏的改變做出決定。他們以一個基於輪爲過程的投票進行。在每一輪中,每一位參議員均可以行使兩項權利中的一
項:
禁止一名參議員的權利
:
參議員可讓另外一位參議員在這一輪和隨後的幾輪中喪失全部的權利。
宣佈勝利
:若是參議員發現有權利投票的參議員都是同一個陣營的,他能夠宣佈勝利並決定在遊戲中的有關變化。
給定一個字符串表明每一個參議員的陣營。字母「R」和「D」分別表明了Radiant
(天輝)和 Dire
(夜魘)。而後,若是有 n 個參議員,給定字符串的大小將是n
。
以輪爲基礎的過程從給定順序的第一個參議員開始到最後一個參議員結束。這一過程將持續到投票結束。全部失去權利的參議員將在過程當中被跳過。
假設每一位參議員都足夠聰明,會爲本身的政黨作出最好的策略,你須要預測哪一方最終會宣佈勝利並在 Dota2 遊戲中決定改變。輸出應該是 Radiant
或 Dire
。
示例 1:
輸入: "RD" 輸出: "Radiant" 解釋: 第一個參議員來自 Radiant 陣營而且他可使用第一項權利讓第二個參議員失去權力,所以第二個參議員將被跳過由於他沒有任何權利。而後在第二輪的時候,第一個參議員能夠宣佈勝利,由於他是惟一一個有投票權的人
示例 2:
輸入: "RDD" 輸出: "Dire" 解釋: 第一輪中,第一個參議員可使用第一項權利禁止第二個參議員的權利 第二個參議員會被跳過由於他的權利被禁止 第三個參議員可使用他的第一項權利禁止第一個參議員的權利 所以在第二輪只剩下第三個參議員擁有投票的權利,因而他能夠宣佈勝利 來自 Radiant 陣營的來自 Dire 陣營的來自 Dire 陣營的
注意:
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 }