Queue· 25 min

Dota2 Senate

LeetCode #649 · Medium · Interactive step-by-step visualization

Setup

Example inputsenate = "RDDRRD"
Step 1 / 15

Current step

1 / 15

Race two queues. A senator bans the next opponent in turn order, so the earlier index always wins the exchange. Split the senate into one queue per party, keeping original indices.

Step log

senate = "RDDRRD"

R
0
D
1
D
2
R
3
R
4
D
5

Radiant queue (index order)

front · exitemptyrear · enter

Dire queue (index order)

front · exitemptyrear · enter

round

1

Named pointer
Active node
Committed or completed
Detached or removed
dota2-senate.java
1public String predictPartyVictory(String senate) {
2 Queue<Integer> radiant = new LinkedList<>();
3 Queue<Integer> dire = new LinkedList<>();
4 int n = senate.length();
5 for (int i = 0; i < n; i++)
6 if (senate.charAt(i) == 'R') radiant.add(i); else dire.add(i);
7 while (!radiant.isEmpty() && !dire.isEmpty()) {
8 int r = radiant.poll(), d = dire.poll();
9 if (r < d) radiant.add(r + n); else dire.add(d + n);
10 }
11 return radiant.isEmpty() ? "Dire" : "Radiant";
12}
● Java
Time
O(n)
Space
O(n)
Pattern
queue
Frames
15