Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 649. Dota2 Senate #249

Open
Woodyiiiiiii opened this issue May 4, 2023 · 0 comments
Open

Leetcode 649. Dota2 Senate #249

Woodyiiiiiii opened this issue May 4, 2023 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

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";
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant