为什么80%的码农都做不了架构师?>>>   

问题:

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

解决:

① 模拟了刀塔类游戏开始之前的BP过程,两个阵营按顺序Ban掉对方的英雄,看最后谁剩下来了,就返回哪个阵营。

先统计所有R和D的个数,然后从头开始遍历,如果遇到了R,就扫描环行链之后的所有位置,其实就是坐标对总长度取余,使其不会越界,如果我们找到了下一个D,就将其标记为B,然后对应的计数器cntR自减1。对于D也是同样处理,我们的while循环的条件是cntR和cntD都要大于0,当有一个等于0了的话,那么退出循环,返回那个不为0的阵营即可。

class Solution { //750ms
    public String predictPartyVictory(String senate) {
        int len = senate.length();
        int countR = 0;
        int countD = 0;
        for (char c : senate.toCharArray()){
            if (c == 'R'){
                countR ++;
            }else {
                countD ++;
            }
        }
        if (countR == 0) return "Dire";
        if (countD == 0) return "Radiant";
        char[] schar = senate.toCharArray();
        while(countR > 0 && countD > 0){
            for (int i = 0;i < len;i ++){
                if (schar[i] == 'R'){
                    for (int j = i + 1;j < i + len;j ++){
                        if (schar[j % len] == 'D'){
                            schar[j % len] = 'B';
                            countD --;
                            break;
                        }
                    }
                }else if (schar[i] == 'D'){
                    for (int j = i + 1;j < i + len;j ++){
                        if (schar[j % len] == 'R'){
                            schar[j % len] = 'B';
                            countR --;
                            break;
                        }
                    }
                }
            }
        }
        return countR == 0 ? "Dire" : "Radiant";
    }
}

② 使用两个队列保存两个阵营各自的位置。

class Solution { //24ms
    public String predictPartyVictory(String senate) {
        int len = senate.length();
        Queue<Integer> q1 = new LinkedList<>();
        Queue<Integer> q2 = new LinkedList<>();
        for (int i = 0;i < len;i ++){
            if (senate.charAt(i) == 'R'){
                q1.offer(i);
            }else{
                q2.offer(i);
            }
        }
        while(! q1.isEmpty() && ! q2.isEmpty()){
            int i = q1.poll();
            int j = q2.poll();
            if (i < j){
                q1.offer(i + len);
            }else {
                q2.offer(j + len);
            }
        }
        return q1.size() > q2.size() ? "Radiant" : "Dire";
    }
}

③ 快慢指针

class Solution { //12ms
    public String predictPartyVictory(String senate) {
        char[] schar = senate.toCharArray();
        int len = schar.length;
        int currR = 0;
        int currD = 0;
        while(len > 0){
            int slow = 0;
            int fast = 0;
            int oldLen = len;
            while(fast < len){
                if(schar[fast] == 'D'){
                    if(currR > 0){
                        currR --;
                    }else{
                        currD ++;
                        schar[slow ++] = 'D';
                    }
                }else{
                    if(currD > 0){
                        currD --;
                    }else{
                        currR ++;
                        schar[slow ++] = 'R';
                    }
                }
                fast ++;
            }
            len = slow;
            if(len == oldLen)return schar[0] == 'D' ? "Dire" : "Radiant";
        }
        return "";
    }
}

转载于:https://my.oschina.net/liyurong/blog/1606463

最后谁剩下来了就返回哪个阵营 Dota2 Senate相关推荐

  1. LeetCode简单题之最后一块石头的重量

    题目 有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块 最重的 石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x = ...

  2. 亿级流量架构实战之秒杀设计

    前面已经写了很多亿级流量的文章, 中间讲了各种处理思路, 这儿将这些思路与业务综合起来, 情形一就是秒杀, 提到秒杀, 很多人都会觉得这是一件技术要求很高的事情, 因为这涉及到超大访问量(可能瞬间千万 ...

  3. access两字段同时升序排序_7 天时间,我整理并实现了这 9 种常见的排序算法

    排序算法 回顾 我们前面已经介绍了 3 种最常见的排序算法: java 实现冒泡排序讲解 QuickSort 快速排序到底快在哪里? SelectionSort 选择排序算法详解(java 实现) 然 ...

  4. Swift反射API及其用法

    尽管 Swift 一直在强调强类型.编译时安全和静态调度,但它的标准库仍然提供了反射机制.可能你已经在很多博客文章或者类似Tuples.Midi Packets和Core Data的项目中见过它.也许 ...

  5. php 数组去重_数组去重(JavaScript 为例)

    数组去重,就是在数组中查找相同的元素,保留其中一个,去除其他元素的程. 从这句话揭示了数组去重的两个关键因素: 找到重复项 去除重复项 本文告诉你在遇到去重问题时该如何思考,并以 JavaScript ...

  6. 【C 语言】指针 与 数组 ( 指针 | 数组 | 指针运算 | 数组访问方式 | 字符串 | 指针数组 | 数组指针 | 多维数组 | 多维指针 | 数组参数 | 函数指针 | 复杂指针解读)

    相关文章链接 : 1.[嵌入式开发]C语言 指针数组 多维数组 2.[嵌入式开发]C语言 命令行参数 函数指针 gdb调试 3.[嵌入式开发]C语言 结构体相关 的 函数 指针 数组 4.[嵌入式开发 ...

  7. Android TV 快进帧预览

    前言 入职新公司,刚接手的AndroidTV项目,初次接触TV开发,被各种骚东西搞得头皮发麻,写点东西记录一下. 正文 MediaMetadataRetriever 1. 获取视频帧的关键类:Medi ...

  8. LeetCode-动态规划背包题-1049. 最后一块石头的重量 II

    描述 1049. 最后一块石头的重量 II 有一堆石头,用整数数组 stones 表示.其中 stones[i] 表示第 i 块石头的重量. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石 ...

  9. ML之DT之CART:分类与回归树CART算法的简介、应用、经典案例之详细攻略

    ML之DT之CART:分类与回归树CART算法的简介.应用.经典案例之详细攻略 目录 分类与回归树CART算法简介 1.CART原理-比较ID3.C4.5 2.CART算法描述 CART算法的案经典案 ...

最新文章

  1. 人工智能顶级会议ICLR取消线下会议:远程出席、视频演讲
  2. 欧盟最新《AI网络安全政策发展框架》
  3. 99%网工都会遇到的10道经典面试问题
  4. 条件概率的几何解释 由定义计算条件概率 由条件概率公式计算条件概率
  5. 实验4 进程运行轨迹的跟踪与统计
  6. android viewpage预加载和懒加载问题
  7. mysql8 设置了默认值 CURRENT_TIMESTAMP 依然报null问题
  8. 六级词汇打卡第天四天(四)
  9. python print return_对python中return和print的一些理解
  10. 【JavaScript】javaScript基础知识回顾
  11. Rust : 如何use本地化crate与 dependencies 和 path
  12. Tensorflow2.0 之 SSD 网络结构
  13. 基于Mybatis的语音播报随机点到系统
  14. 【Mapreduce】利用job嵌套,多重Mapreduce,求解二度人脉
  15. 近景摄影测量空间后方交会python
  16. C# UDP Socket ReceiveFrom 远程主机强迫关闭了一个现有的连接。
  17. 密码学复习笔记2【分组密码/S-DES、DES】
  18. 知乎百万热议:为什么涨薪也只能靠跳槽?老板都宁愿高薪聘请外人也不愿加薪?
  19. matlab将彩图转化成灰度图,matlab 如何将彩图转成灰度图
  20. Python读取excel中的图片

热门文章

  1. linux下IIC驱动解释
  2. 共享文件夹:请检查名称的拼写,否则,网络可能有问题错误代码0x80070035
  3. 计算机英语情景对话二人组,英语小对话二人组日常情景对话
  4. 天宇优配|沪指冲高回落跌0.35%,地产、医药等板块走强,供销社概念再创新高
  5. MongoDB之查询文档
  6. 谷歌账户暂停三个月重新启用,谷歌账户暂停三个月重新启用,转化目标是否有效?
  7. redis分布式方案redis cluster的介绍和实践
  8. linux java性能监控工具_常用Linux 性能监测工具
  9. java从接口直接下载文件到本地
  10. 无线网卡驱动突然坏了怎么办