LeetCode 75

Dota2 Senate

Problem
LC 649
Topic
Queue
File
official75_LC649Dota2Senate.java
Path
pkg5leetcode/official75/official75_LC649Dota2Senate.java
Package
pkg5leetcode.official75
Command
java pkg5leetcode/official75/official75_LC649Dota2Senate.java
Approach
Two queues simulate ban order by index.
Complexity
Time O(n), Space O(n)

LeetCode solutions

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg5leetcode/official75/official75_LC649Dota2Senate.java
1package pkg5leetcode.official75;2 3/*4 * Dota2 Senate | LC 6495 * APPROACH: Two queues simulate ban order by index.6 * COMPLEXITY: Time O(n), Space O(n)7 */8import java.util.*;9 10public class official75_LC649Dota2Senate {11    static String predictPartyVictory(String senate) {12        int n = senate.length();13        Deque<Integer> r = new ArrayDeque<>(), d = new ArrayDeque<>();14        for (int i = 0; i < n; i++)15            if (senate.charAt(i) == 'R') r.addLast(i); else d.addLast(i);16        while (!r.isEmpty() && !d.isEmpty()) {17            int ri = r.pollFirst(), di = d.pollFirst();18            if (ri < di) r.addLast(ri + n); else d.addLast(di + n);19        }20        return r.isEmpty() ? "Dire" : "Radiant";21    }22 23    public static void main(String[] args) {24        check("Radiant".equals(predictPartyVictory("RD")), "case1");25        check("Dire".equals(predictPartyVictory("RDD")), "case2");26        System.out.println("all tests passed");27    }28 29    static void check(boolean cond, String name) {30        if (!cond) throw new AssertionError("FAILED: " + name);31        System.out.println("  PASS " + name);32    }33}