Leetcode 649.Dota2参议院
Dota2参议院
Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇)
Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的一项:
- 禁止一名参议员的权利:
参议员可以让另一位参议员在这一轮和随后的几轮中丧失所有的权利。
- 宣布胜利:如果参议员发现有权利投票的参议员都是同一个阵营的,他可以宣布胜利并决定在游戏中的有关变化。
给定一个字符串代表每个参议员的阵营。字母"R"和"D"分别代表了Radiant(天辉)和 Dire(夜魇)。然后,如果有 n 个参议员,给定字符串的大小将是n。
以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。
假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在 Dota2 游戏中决定改变。输出应该是 Radiant 或 Dire。
示例 1:
输入: "RD"
输出: "Radiant"
解释: 第一个参议员来自 Radiant 阵营并且他可以使用第一项权利让第二个参议员失去权力,因此第二个参议员将被跳过因为他没有任何权利。然后在第二轮的时候,第一个参议员可以宣布胜利,因为他是唯一一个有投票权的人
示例 2:
输入: "RDD"
输出: "Dire"
解释:
第一轮中,第一个来自 Radiant 阵营的参议员可以使用第一项权利禁止第二个参议员的权利
第二个来自 Dire 阵营的参议员会被跳过因为他的权利被禁止
第三个来自 Dire 阵营的参议员可以使用他的第一项权利禁止第一个参议员的权利
因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利
注意:
- 给定字符串的长度在 [1, 10,000] 之间.
思路
Intuition
A senator performing a ban doesn't need to use it on another senator immediately. We can wait to see when another team's senator will vote, then use that ban retroactively.
Algorithm
Put the senators in an integer queue: 1 for 'Radiant' and 0 for 'Dire'.
Now process the queue: if there is a floating ban for that senator, exercise it and continue. Otherwise, add a floating ban against the other team, and enqueue this senator again.
import java.util.LinkedList;
import java.util.Queue; class Solution {
public String predictPartyVictory(String senate) {
Queue<Integer> queue = new LinkedList();
int[] people = new int[]{0, 0};
int[] bans = new int[]{0, 0}; for (char person: senate.toCharArray()) {
int x = person == 'R' ? 1 : 0;
people[x]++;
queue.add(x);
} while (people[0] > 0 && people[1] > 0) {
int x = queue.poll();
if (bans[x] > 0) {
bans[x]--;
people[x]--;
} else {
bans[x^1]++;
queue.add(x);
}
} return people[1] > 0 ? "Radiant" : "Dire";
}
}
Leetcode 649.Dota2参议院的更多相关文章
- Java实现 LeetCode 649 Dota2 参议院(暴力大法)
649. Dota2 参议院 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决 ...
- 【力扣】649. Dota2 参议院
Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决定.他们以一个基于轮为过程的投 ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- [Swift]LeetCode649. Dota2 参议院 | Dota2 Senate
In the world of Dota2, there are two parties: the Radiantand the Dire. The Dota2 senate consists of ...
- 649. Dota2 Senate
In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode刷题总结-二分查找和贪心法篇
本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 小图示优化 - ASP.NET Sprite and Image Optimization (Web Form)
小图示优化 - ASP.NET Sprite and Image Optimization (Web Form) 透过 NuGet安装下面的套件,可以将您的小图示(icon)合并成一张图 透过 CSS ...
- 使用SAP云平台 + JNDI访问Internet Service
以Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destin ...
- [Rails学习之路]初识Ruby(二)
继续上次Ruby的学习.接下来就到了Ruby的方法. Ruby的方法与Python仍然很像.使用def定义,可以使用undef取消定义. 在Ruby中,经常可以看见方法后面跟有"?" ...
- IOS UIActivityIndicatorView动画
● 是一个旋转进度轮,可以用来告知用户有一个操作正在进行中,一般 用initWithActivityIndicatorStyle初始化 ● 方法解析: ● - (void)startAnimating ...
- IOS 自定义Layer(图层)
方式1: @interface NJViewController () @end @implementation NJViewController - (void)viewDidLoad { [sup ...
- UVA 12169 Disgruntled Judge(Extended_Euclid)
用扩展欧几里德Extended_Euclid解线性模方程,思路在注释里面了. 注意数据范围不要爆int了. /********************************************* ...
- autoreleasing on a thread
So basically, if you are running on OS X 10.9+ or iOS 7+, autoreleasing on a thread without a pool s ...
- Portal的认证方式
不同的组网方式下,可采用的 Portal 认证方式不同.按照网络中实施 Portal 认证的 网络层次来分,Portal 的认证方式分为两种:二层认证方式和三层认证方式. 二层认证方式.这种方式支持在 ...
- python之列表推导、迭代器、生成器
http://blog.chinaunix.net/uid-26722078-id-3484197.html 1.列表推导 看几个例子,一切就明白了. #!/usr/bin/python number ...
- solr dataimport
solrconfig.xml <requestHandler name="/dataimport" class="org.apache.solr.handler.d ...