649. Dota2 Senate
In the world of Dota2, there are two parties: the Radiant
and 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:
Ban one senator's right
:
A senator can make another senator lose all his rights in this and all the following rounds.Announce the victory
:
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:
- The length of the given string will in the range [1, 10,000].
Approach #1: C++.
- class Solution {
- public:
- string predictPartyVictory(string senate) {
- int len = senate.length();
- queue<int> q1, q2;
- for (int i = 0; i < len; ++i)
- senate[i] == 'R' ? q1.push(i) : q2.push(i);
- while (!q1.empty() && !q2.empty()) {
- int r_index = q1.front();
- int d_index = q2.front();
- q1.pop(), q2.pop();
- r_index < d_index ? q1.push(r_index + len) : q2.push(d_index + len);
- }
- return q1.size() > q2.size() ? "Radiant" : "Dire";
- }
- };
Analysis;
we use two queue to maintion the index of 'R' and 'D' in the senate. In every loop we compare the two front elements in these queue. we push the little one add len into the original queue because he can vote in the next round. If one of the queue is empty we compare the size of these queue, and return the answar.
649. Dota2 Senate的更多相关文章
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- Java实现 LeetCode 649 Dota2 参议院(暴力大法)
649. Dota2 参议院 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决 ...
- [LeetCode] Dota2 Senate 刀塔二参议院
In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...
- [Swift]LeetCode649. Dota2 参议院 | Dota2 Senate
In the world of Dota2, there are two parties: the Radiantand the Dire. The Dota2 senate consists of ...
- Leetcode 649.Dota2参议院
Dota2参议院 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决定.他们以一 ...
- 【力扣】649. Dota2 参议院
Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决定.他们以一个基于轮为过程的投 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- windows下使用nginx
本文介绍如何在windows下使用nginx 起步 下载安装 将nginx安装成windows服务 常用命令 构建服务 静态服务 代理服务器 http配置文件转移 负载均衡 负载均衡配置 负载均衡方法 ...
- cs231n线性分类器作业 svm代码 softmax
CS231n之线性分类器 斯坦福CS231n项目实战(二):线性支持向量机SVM CS231n 2016 通关 第三章-SVM与Softmax cs231n:assignment1——Q3: Impl ...
- Python 小知识点(1)
1.Python命名规则------>下划线连接 girl_of_wfb="lgl" 2.常量-----名称全大写->WFB="WFaceBoss&qu ...
- requirejs——基础
一.requirejs存在的意义: 我们引用外部JS文件通常是这样引用的: <script src="1.js"></script> <script ...
- 切面(Aspect)获取请求参数和返回值
@Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 接收到请求, ...
- wordpress 基础文件
需要用到的PHP基础文件有: 404.php 404模板 rtl.css 如果网站的阅读方向是自右向左的,会被自动包含进来 comments.php 评论模板 single.php 文章模板.显 ...
- winform问题集锦
正试图在 os 加载程序锁内执行托管代码.不要尝试在 DllMain 或映像初始化函数内运行托管代码 说明 .NET2.0中增加了42种非常强大的调试助手,MDA.Loaderlock 是其中之一.L ...
- 一卡通大冒险(hdu 2512)
因为长期钻研算法, 无暇顾及个人问题,BUAA ACM/ICPC 训练小组的帅哥们大部分都是单身.某天,他们在机房商量一个绝妙的计划"一卡通大冒险".这个计划是由wf最先提出来的, ...
- oracle数据库导入导出数据
导出命令 exp username/password@192.168.x.xx/orcl file='D:\20170126.dmp' log='D:\20170126.log' 导入命令 imp u ...
- java基础篇之HashMap
HashMap和C#中的Dictionary基本一样 键是唯一值 值可以是对象 循环HashMap的方式:一: 1,通过Set<T> st = hs.keySet()找到所有的key值集合 ...