Dota2 Senate

medium queue greedy

Problem

In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game.

Inputsenate = "RDD"
Output"D"
R bans D[1]; D[2] then bans R[0]; D wins.

from collections import deque
def predict_party_victory(s):
    r, d = deque(), deque()
    n = len(s)
    for i, c in enumerate(s):
        (r if c == 'R' else d).append(i)
    while r and d:
        ri, di = r.popleft(), d.popleft()
        if ri < di: r.append(ri + n)
        else: d.append(di + n)
    return "R" if r else "D"
function predictPartyVictory(s) {
  const r = [], d = [], n = s.length;
  for (let i = 0; i < n; i++) (s[i] === 'R' ? r : d).push(i);
  while (r.length && d.length) {
    const ri = r.shift(), di = d.shift();
    if (ri < di) r.push(ri + n); else d.push(di + n);
  }
  return r.length ? "R" : "D";
}
class Solution {
    public String predictPartyVictory(String s) {
        Deque<Integer> r = new ArrayDeque<>(), d = new ArrayDeque<>();
        int n = s.length();
        for (int i = 0; i < n; i++) (s.charAt(i) == 'R' ? r : d).offer(i);
        while (!r.isEmpty() && !d.isEmpty()) {
            int ri = r.poll(), di = d.poll();
            if (ri < di) r.offer(ri + n); else d.offer(di + n);
        }
        return !r.isEmpty() ? "R" : "D";
    }
}
string predictPartyVictory(string s) {
    queue<int> r, d;
    int n = s.size();
    for (int i = 0; i < n; i++) (s[i] == 'R' ? r : d).push(i);
    while (!r.empty() && !d.empty()) {
        int ri = r.front(); r.pop();
        int di = d.front(); d.pop();
        if (ri < di) r.push(ri + n); else d.push(di + n);
    }
    return !r.empty() ? "R" : "D";
}
Time: O(n) amortised Space: O(n)