题目

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].

思路

这是一道贪心算法的题目:对于每个议员而言,最好的策略就是禁止对方党派下一个议员行使权力,所以每次就用贪心算法找到下一个对方党派的议员,并删除。这样不断迭代,直到某一方党派的人全部被删除。在实现中,我们用两个队列来模拟两个党派的人:取出两个队首元素,看看谁行使权力的时间更靠前,然后将行使权力时间靠前的议员从队首移到队尾,将时间靠后的议员直接从队列中删除。如何判断队员行使权力的时间呢?我们可以在队列中用int来表示行使权力的顺序号,每次判断两个队列中int的大小即可。

代码

class Solution {
public:string predictPartyVictory(string senate) {queue<int> q1, q2;int n = senate.length();for (int i = 0; i < n; ++i) {senate[i] == 'R' ? q1.push(i) : q2.push(i);}while (!q1.empty() && !q2.empty()) {int r_index = q1.front(), d_index = q2.front();q1.pop(), q2.pop();(r_index < d_index) ? q1.push(r_index + n) : q2.push(d_index + n);}return q1.size() > q2.size() ? "Radiant" : "Dire";}
};

[Leetcode] 649. Dota2 Senate 解题报告相关推荐

  1. LeetCode 649. Dota2 参议院 | Python

    649. Dota2 参议院 题目来源:力扣(LeetCode)https://leetcode-cn.com/problems/dota2-senate/ 题目 Dota2 的世界里有两个阵营:Ra ...

  2. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

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

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

  4. LeetCode 649. Dota2 参议院(循环队列)

    文章目录 1. 题目 2. 解题 1. 题目 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇) Dota2 参议院由来自两派的参议员组成.现在参议院希望对一个 Dota2 游 ...

  5. LeetCode 649 Dota2参议院 HERODING的LeetCode之路

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

  6. leetcode 649. Dota2 参议院(贪心算法)

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

  7. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  8. 【LeetCode】77. Combinations 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  9. Leetcode 649.Dota2参议院

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

最新文章

  1. Ocelot(一)- .Net Core开源网关
  2. 简述osi参考模型各层主要功能_计软考研双日练 | OSI参考模型各层提供什么服务?...
  3. 【强化学习】DQN 的三种改进在运筹学中的应用
  4. GBrowse配置相关资料
  5. [SpringSecurity]web权限方案_用户认证_自定义用户登录页面
  6. 使用python和pandas进行同类群组分析
  7. 【推荐实践】推荐系统中模型训练及使用流程的标准化
  8. design php 如何使用ant_Ant Design Pro如何调用接口 | Ant Design Pro渲染数据 绑定数据...
  9. 番禺区天气预报软件测试,天气预报模块测试用例(P707)
  10. 慎用PHP的unset、array_unique方法
  11. 研究生数学建模竞赛准备
  12. 拼多多发单软件使用教程永久免费
  13. SOLD格雷母线是什么?
  14. 还在傻傻的数star、数fork吗?3秒钟教会你如何查看GitHub项目活跃度,是死是活一眼便知
  15. 两年多工作心得和体会
  16. 渐进式 Web 应用程序介绍
  17. php微信公众号支付实例教程,PHP微信公众号支付教程(含图文)
  18. hdmi网线延长器_HDMI单网线延长器的制作方法
  19. 【JZOJ6150】爱乐之城
  20. 技术项目的评审及其标准

热门文章

  1. 二进制与十进制整数,浮点数相互转换
  2. excel函数-ceiling函数用法(向上取某个值的倍数)
  3. 如何利用BetterZip Mac版筛选过滤压缩文件?
  4. 零基础上手unity VR开发【Oculus账号体系准备】
  5. 查询一个部门员工超过六人的部门名称
  6. Ceph运维存储 命令管理操作
  7. 地下城与勇士手游服务器一直维护是为什么,地下城与勇士手游版进不去 DNF手游进不去解决方法...
  8. 冬天宝宝洗澡,水温、时间和次数的讲究,你知道吗?
  9. Javascript网页打印方法汇总
  10. 解决docker中发布到docker-hub上The push refers to a repository的报错原因。