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:

  1. Ban one senator's right
    A senator can make another senator lose all his rights in this and all the following rounds.
  2. 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:

  1. 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的更多相关文章

  1. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  2. Java实现 LeetCode 649 Dota2 参议院(暴力大法)

    649. Dota2 参议院 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决 ...

  3. [LeetCode] Dota2 Senate 刀塔二参议院

    In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...

  4. [Swift]LeetCode649. Dota2 参议院 | Dota2 Senate

    In the world of Dota2, there are two parties: the Radiantand the Dire. The Dota2 senate consists of ...

  5. Leetcode 649.Dota2参议院

    Dota2参议院 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决定.他们以一 ...

  6. 【力扣】649. Dota2 参议院

    Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游戏里的改变作出决定.他们以一个基于轮为过程的投 ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. DoDataExchange函数,UpdateData(TRUE)和UpdateData(FALSE)的区别

    MFC控件(暂时为Edit控件)与数据的绑定,变量值可以在界面和后台之间传递. 我们在DoDataExchange(CDataExchange* pDX) 函数里,实现了MFC控件和变量的绑定.  若 ...

  2. python写一个能生成三种一句话木马的脚本

    代码: import time import os from threading import Thread import optparse def aspyijuhua(): try: juy=op ...

  3. MFC 控件使用汇总

    一.动态创建button CButton *button=new CButton; button->Create(_T(,,,),);//最后一个是ID BEGIN_MESSAGE_MAP(CM ...

  4. 用python40行代码编写的计算器

    效果图 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

  5. spring mvc学习 总体概览

      spring mvc 设计概览   springmvc处理http请求,主要是在web.xml中配置一个dispatcherservlet,然后由此进行拦截并处理请求返回相应,下面就针对源码大体记 ...

  6. OO设计基本原则

    OO本身就是一种大的设计模式,它是随着软件规模越来越大产生出来帮助人们建模和开发的理念,生来就带着封装.继承.多态等可复用基因.为了充分发挥这些基因的功效,使用者需要遵守一定的原则,就是所谓的面向对象 ...

  7. Linux 学习笔记之 --- epoll 事件模型详解

    epoll 主要采用对已就绪的 fd 进行轮询操作   一.epoll 触发方式 epoll支持 ET 和 LT 两种触发方式 ET(边缘触发):Nginx 就是采用 ET 触发方式,只支持 no-b ...

  8. : error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

    打开项目----项目属性---配置属性----C/C++ ----预处理器----预处理定义,添加_CRT_SECURE_NO_WARNINGS

  9. omitTermFreqAndPositions设置,词频FQ在打分中默认为1

    compressed=true|false,是否使用gzip压缩(只有TextField和StrField可以压缩) compressThreshold=压缩阀值 multiValued=true|f ...

  10. Windows:condition_variable 两个例子

    题目:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码.注意:一定是子线程先执行,主线程再执行. #in ...