QueueMedium

Dota2 Senate Visualizer

What is Dota2 Senate

Senators are R or D in a circle. The next senator who has not been banned bans one senator of the other party. The party that bans every opponent wins.

Approach

While both queues have someone, the earlier senator bans the other front and goes to the end of their own queue.

Visualization & algorithm

Radiant and Dire still able to act
R queue and D queue of indexes
while both are nonempty:
the smaller index bans the other
that senator returns at index + n
the party that remains wins
Step 0/0 Answer —

The finished picture is on screen when the page opens. Next and Play start from the first step. The controls sit under the picture and the pseudocode.

Complexity

PieceCostWhy
TimeO(n)Each senator is banned at most once and acts at most once more.
SpaceO(n)The two queues.

Implementation

from collections import deque
def solve(self, senate):
    r = deque(i for i,c in enumerate(senate) if c=='R')
    d = deque(i for i,c in enumerate(senate) if c=='D')
    n = len(senate)
    while r and d:
        if r[0] < d[0]:
            r.append(r.popleft()+n); d.popleft()
        else:
            d.append(d.popleft()+n); r.popleft()
    return 'Radiant' if r else 'Dire'