649. Dota2 Senate
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem describes a voting process in the Dota2 senate with two parties, Radiant ('R') and Dire ('D'). Senators vote in rounds, following a fixed order. In each senator's turn, they can ban an opponent, permanently removing their right to vote. The goal is to determine which party will be the last one standing.
Solution
Dota2 Senate in Python
from collections import deque
class Solution:
def predictPartyVictory(self, senate: str) -> str:
n = len(senate)
# Queues to store the indices of senators from each party.
radiant_q = deque()
dire_q = deque()
# Initialize the queues with senator indices.
for i, party in enumerate(senate):
if party == 'R':
radiant_q.append(i)
else:
dire_q.append(i)
# Simulate the voting rounds until one party is eliminated.
while radiant_q and dire_q:
# Get the indices of the next voting senator from each party.
r_idx = radiant_q.popleft()
d_idx = dire_q.popleft()
# The senator with the smaller index gets to vote first.
if r_idx < d_idx:
# Radiant senator bans the Dire senator.
# The Radiant senator gets to vote again in the next round.
# We add n to their index to signify this.
radiant_q.append(r_idx + n)
else:
# Dire senator bans the Radiant senator.
# The Dire senator gets to vote again in the next round.
dire_q.append(d_idx + n)
# The party with remaining senators wins.
return "Radiant" if radiant_q else "Dire"
Dota2 Senate in Java
class Solution {
public String predictPartyVictory(String senate) {
int n = senate.length();
// Queues to store the indices of senators from each party.
Queue<Integer> radiantQ = new LinkedList<>();
Queue<Integer> direQ = new LinkedList<>();
// Initialize the queues with senator indices.
for (int i = 0; i < n; i++) {
if (senate.charAt(i) == 'R') {
radiantQ.add(i);
} else {
direQ.add(i);
}
}
// Simulate the voting rounds until one party is eliminated.
while (!radiantQ.isEmpty() && !direQ.isEmpty()) {
// Get the indices of the next voting senator from each party.
int rIdx = radiantQ.poll();
int dIdx = direQ.poll();
// The senator with the smaller index gets to vote first.
if (rIdx < dIdx) {
// Radiant senator bans the Dire senator.
// The Radiant senator gets to vote again in the next round.
// We add n to their index to signify this.
radiantQ.add(rIdx + n);
} else {
// Dire senator bans the Radiant senator.
// The Dire senator gets to vote again in the next round.
direQ.add(dIdx + n);
}
}
// The party with remaining senators wins.
return radiantQ.isEmpty() ? "Dire" : "Radiant";
}
}
Dota2 Senate in C++
class Solution {
public:
string predictPartyVictory(string senate) {
int n = senate.length();
// Queues to store the indices of senators from each party.
queue<int> radiant_q, dire_q;
// Initialize the queues with senator indices.
for (int i = 0; i < n; ++i) {
if (senate[i] == 'R') {
radiant_q.push(i);
} else {
dire_q.push(i);
}
}
// Simulate the voting rounds until one party is eliminated.
while (!radiant_q.empty() && !dire_q.empty()) {
// Get the indices of the next voting senator from each party.
int r_idx = radiant_q.front();
radiant_q.pop();
int d_idx = dire_q.front();
dire_q.pop();
// The senator with the smaller index gets to vote first.
if (r_idx < d_idx) {
// Radiant senator bans the Dire senator.
// The Radiant senator gets to vote again in the next round.
// We add n to their index to signify this.
radiant_q.push(r_idx + n);
} else {
// Dire senator bans the Radiant senator.
// The Dire senator gets to vote again in the next round.
dire_q.push(d_idx + n);
}
}
// The party with remaining senators wins.
return radiant_q.empty() ? "Dire" : "Radiant";
}
};
Dota2 Senate in TypeScript
function predictPartyVictory(senate: string): string {
const n = senate.length;
// Queues to store the indices of senators from each party.
const radiantQ: number[] = [];
const direQ: number[] = [];
// Initialize the queues with senator indices.
for (let i = 0; i < n; i++) {
if (senate[i] === 'R') {
radiantQ.push(i);
} else {
direQ.push(i);
}
}
// Pointers for efficient queue simulation using arrays.
let rHead = 0;
let dHead = 0;
// Simulate the voting rounds until one party is eliminated.
// radiantQ.length and direQ.length grow, so we use pointers to track consumed elements.
while (rHead < radiantQ.length && dHead < direQ.length) {
// Get the indices of the next voting senator from each party.
const rIdx = radiantQ[rHead++];
const dIdx = direQ[dHead++];
// The senator with the smaller index gets to vote first.
if (rIdx < dIdx) {
// Radiant senator bans the Dire senator.
// The Radiant senator gets to vote again in the next round.
// We add n to their index and push to the end of the queue.
radiantQ.push(rIdx + n);
} else {
// Dire senator bans the Radiant senator.
// The Dire senator gets to vote again in the next round.
direQ.push(dIdx + n);
}
}
// The party with remaining (unconsumed) senators wins.
return rHead < radiantQ.length ? "Radiant" : "Dire";
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.