Leetcode_649_参议院_模拟_贪心

12/11

用队列模拟
class Solution {
    
    
    public String predictPartyVictory(String senate) {
    
    
        int n = senate.length();
        Queue<Integer> R = new LinkedList<>();
        Queue<Integer> D = new LinkedList<>();
        for (int i = 0; i < n; i++) {
    
    
            if (senate.charAt(i) == 'R') {
    
    
                R.offer(i);
            } else {
    
    
                D.offer(i);
            }
        }
        while (!R.isEmpty()&&!D.isEmpty()) {
    
    
            int Rfront = R.poll();
            int Dfront = D.poll();
            if (Rfront < Dfront) {
    
    
                R.offer(Rfront + n);
            } else {
    
    
                D.offer(Dfront + n);
            }
        }
        return R.isEmpty()?"Dire":"Radiant";
    }
}

猜你喜欢

转载自blog.csdn.net/HDUCheater/article/details/111028114