[Swift]LeetCode649. Dota2 参议院 | Dota2 Senate
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/10485423.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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 的世界里有兩個(gè)陣營(yíng):Radiant(天輝)和?Dire(夜魘)
Dota2 參議院由來(lái)自兩派的參議員組成。現(xiàn)在參議院希望對(duì)一個(gè) Dota2 游戲里的改變作出決定。他們以一個(gè)基于輪為過(guò)程的投票進(jìn)行。在每一輪中,每一位參議員都可以行使兩項(xiàng)權(quán)利中的一項(xiàng):
禁止一名參議員的權(quán)利:
參議員可以讓另一位參議員在這一輪和隨后的幾輪中喪失所有的權(quán)利。
宣布勝利:如果參議員發(fā)現(xiàn)有權(quán)利投票的參議員都是同一個(gè)陣營(yíng)的,他可以宣布勝利并決定在游戲中的有關(guān)變化。
給定一個(gè)字符串代表每個(gè)參議員的陣營(yíng)。字母“R”和“D”分別代表了Radiant(天輝)和?Dire(夜魘)。然后,如果有 n 個(gè)參議員,給定字符串的大小將是n。
以輪為基礎(chǔ)的過(guò)程從給定順序的第一個(gè)參議員開(kāi)始到最后一個(gè)參議員結(jié)束。這一過(guò)程將持續(xù)到投票結(jié)束。所有失去權(quán)利的參議員將在過(guò)程中被跳過(guò)。
假設(shè)每一位參議員都足夠聰明,會(huì)為自己的政黨做出最好的策略,你需要預(yù)測(cè)哪一方最終會(huì)宣布勝利并在 Dota2 游戲中決定改變。輸出應(yīng)該是?Radiant?或?Dire。
示例 1:
輸入: "RD" 輸出: "Radiant" 解釋: 第一個(gè)參議員來(lái)自 Radiant 陣營(yíng)并且他可以使用第一項(xiàng)權(quán)利讓第二個(gè)參議員失去權(quán)力,因此第二個(gè)參議員將被跳過(guò)因?yàn)樗麤](méi)有任何權(quán)利。然后在第二輪的時(shí)候,第一個(gè)參議員可以宣布勝利,因?yàn)樗俏ㄒ灰粋€(gè)有投票權(quán)的人示例 2:
輸入: "RDD" 輸出: "Dire" 解釋: 第一輪中,第一個(gè)來(lái)自 Radiant 陣營(yíng)的參議員可以使用第一項(xiàng)權(quán)利禁止第二個(gè)參議員的權(quán)利 第二個(gè)來(lái)自 Dire 陣營(yíng)的參議員會(huì)被跳過(guò)因?yàn)樗臋?quán)利被禁止 第三個(gè)來(lái)自 Dire 陣營(yíng)的參議員可以使用他的第一項(xiàng)權(quán)利禁止第一個(gè)參議員的權(quán)利 因此在第二輪只剩下第三個(gè)參議員擁有投票的權(quán)利,于是他可以宣布勝利?注意:
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 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/10485423.html
總結(jié)
以上是生活随笔為你收集整理的[Swift]LeetCode649. Dota2 参议院 | Dota2 Senate的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何将一个数组对象 把对象的值用指定符号
- 下一篇: 虚拟机VM三种网络连接方式说明