zoukankan      html  css  js  c++  java
  • LeetCode 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:

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

     题意概述:

    DOTA2 参议院由Dire和Radiant两派的参议员组成,每一轮的投票中,两边的参议员都有权利使另一派的参议员丧失投票权利或者当所有参议员只剩下自己这一派时宣布胜利,然后让我们求对于输入的一个由R、D组成字符串,其从左至右代表两派参议员的位置顺序,让我们在假设两派参议员都做出最优决策的情况下判断哪一派会胜出。

    算法分析:

    这题很明显是贪心算法,轮到任意一派的参议员最优的决策为让最靠近自己的另一派参议院丧失其接下来的投票权利。因此可以建立两个队列来做,队列的作用就是记录各自门派中参议员的投票次序,位于队首的参议员具有优先投票权且一定是与另一派队首参议员最近。两个队首元素进行比较,位置小的可以使位置大的丧失投票权,然后重新加入到队列中。由于需要循环进行投票,因此在队列中记录的参议员的次序需要不断的发生变化来表示当前的投票次序即所处的位置。

    代码:

    class Solution {
        public String predictPartyVictory(String senate) {
            Queue<Integer> que1 = new LinkedList<Integer>();
            Queue<Integer> que2 = new LinkedList<Integer>();
            int n=senate.length();
            for(int i=0;i<n;i++){
                if(senate.charAt(i)=='R'){
                    que1.offer(i);
                }
                else{
                    que2.offer(i);
                }
            }
            while(que1.peek()!=null&&que2.peek()!=null){
                int a = que1.poll();
                int b = que2.poll();
                if(a<b)
                    que1.offer(a+n);
                else
                    que2.offer(b+n);
            }
            return que1.peek()==null?"Dire":"Radiant";
        }
    }
     
  • 相关阅读:
    Git版本控制使用方法入门教程
    github 与git 使用 及配置
    IOS ASI http 框架详解
    block 理解及 简单回调
    IOS在Xcode 4.x以上如何 创建 和 添加 静态库
    dispatch queues GCD
    理解UIApplication(转)
    android 标签对应的代码怎么写alignParentRight、marginRight
    Android 资源文件的命名规范问题
    输入法弹出,屏幕自动适应
  • 原文地址:https://www.cnblogs.com/Revenent-Blog/p/7922384.html
Copyright © 2011-2022 走看看