在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In the world of Dota2, there are two parties: the 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
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the 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 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 的世界里有两个阵营: Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的
给定一个字符串代表每个参议员的阵营。字母“R”和“D”分别代表了 以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。 假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在 Dota2 游戏中决定改变。输出应该是 示例 1: 输入: "RD"
输出: "Radiant"
解释:
示例 2: 输入: "RDD" 输出: "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 }
|
请发表评论