Woodyiiiiiii / LeetCode

My private record of Leetcode solution

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leetcode 649. Dota2 Senate

Woodyiiiiiii opened this issue · comments

649. Dota2 Senate

649. Dota2 Senate

思维题+模拟题。

好久不见的队列题。贪心**,每个人肯定尽量ban在自己后面的不同阵营的人。所以用队列存储,每次用完后对优先级i+n

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