zoukankan      html  css  js  c++  java
  • Leetcode 649.Dota2参议院

    Dota2参议院

     Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇)

    Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的项:

    1. 禁止一名参议员的权利:

      参议员可以让另一位参议员在这一轮和随后的几轮中丧失所有的权利


    2. 宣布胜利:如果参议员发现有权利投票的参议员都是同一个阵营的,他可以宣布胜利并决定在游戏中的有关变化。

    给定一个字符串代表每个参议员的阵营。字母"R"和"D"分别代表了Radiant(天辉)和 Dire(夜魇)。然后,如果有 n 个参议员,给定字符串的大小将是n。

    以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。

    假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在 Dota2 游戏中决定改变。输出应该是 Radiant 或 Dire。

    示例 1:

    输入: "RD"

    输出: "Radiant"

    解释: 第一个参议员来自 Radiant 阵营并且他可以使用第一项权利让第二个参议员失去权力,因此第二个参议员将被跳过因为他没有任何权利。然后在第二轮的时候,第一个参议员可以宣布胜利,因为他是唯一一个有投票权的人

       

    示例 2:

    输入: "RDD"

    输出: "Dire"

    解释:

    第一轮中,第一个来自 Radiant 阵营的参议员可以使用第一项权利禁止第二个参议员的权利

    第二个来自 Dire 阵营的参议员会被跳过因为他的权利被禁止

    第三个来自 Dire 阵营的参议员可以使用他的第一项权利禁止第一个参议员的权利

    因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利

       

    注意:

    1. 给定字符串的长度在 [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.

     1 import java.util.LinkedList;
     2 import java.util.Queue;
     3 
     4 class Solution {
     5     public String predictPartyVictory(String senate) {
     6         Queue<Integer> queue = new LinkedList();
     7         int[] people = new int[]{0, 0};
     8         int[] bans = new int[]{0, 0};
     9 
    10         for (char person: senate.toCharArray()) {
    11             int x = person == 'R' ? 1 : 0;
    12             people[x]++;
    13             queue.add(x);
    14         }
    15 
    16         while (people[0] > 0 && people[1] > 0) {
    17             int x = queue.poll();
    18             if (bans[x] > 0) {
    19                 bans[x]--;
    20                 people[x]--;
    21             } else {
    22                 bans[x^1]++;
    23                 queue.add(x);
    24             }
    25         }
    26 
    27         return people[1] > 0 ? "Radiant" : "Dire";
    28     }
    29 }
  • 相关阅读:
    [转]进程的用户栈和内核栈
    什么是URL,URL格式
    设计灵感
    Spring源码学习相关记录
    HTML图片标签路径解析
    一次Spring Bean初始化顺序问题排查记录
    是要面向对象,还是简单粗暴?
    2018/07/26学习节点记录
    数据结构-堆 Java实现
    2018 ICPC 徐州邀请赛 总结
  • 原文地址:https://www.cnblogs.com/kexinxin/p/10383077.html
Copyright © 2011-2022 走看看