每日一道Leetcode- 649. Dota2 参议院

在这里插入图片描述

class Solution {
    
    
    public String predictPartyVictory(String senate) {
    
    
        // 记录两个团队的人数
        int n = senate.length();
        // 定义两个队列分别存放radiant的id和dire的id
        Queue<Integer> radiant = new LinkedList<Integer>();
        Queue<Integer> dire = new LinkedList<Integer>();
        // 存入队列
        for(int i = 0;i<n;i++){
    
    
            if(senate.charAt(i)=='R'){
    
    
                radiant.offer(i);
            }else{
    
    
                dire.offer(i);
            }
        }
        // 只要队列均不为空,同时出队列
        while(!radiant.isEmpty() && !dire.isEmpty()){
    
    
            int radiantIndex = radiant.poll();
            int direIndex = dire.poll();
            // 判断当前的index值哪个更小,在前面的更牛一点,前面的还可以再送入队列,id序号直接设置为+n的就可以了
            if(radiantIndex < direIndex){
    
    
                radiant.offer(radiantIndex+n);
            }else{
    
    
                dire.offer(direIndex+n);
            }
        }
        return !radiant.isEmpty()?"Radiant":"Dire";
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41041275/article/details/112182020