649. Dota2 Senate
Problem Description
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 decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:
- Ban one senator's right: A senator can make another senator lose all his rights in this and all the following rounds.
- Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.
Given a string senate representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire".
Examples:
Example 1:
Input: senate = "RD" Output: "Radiant" Explanation: The first senator comes from Radiant and he can just ban the next senator's right in round 1. And the second senator can't exercise any rights anymore since his right has been banned. And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
Example 2:
Input: senate = "RDD" Output: "Dire" Explanation: The first senator comes from Radiant and he can just ban the next senator's right in round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in round 1. And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
Constraints:
- n == senate.length
- 1 ≤ n ≤ 104
- senate[i] is either 'R' or 'D'.
Python Solution
class Solution:
def predictPartyVictory(self, senate: str) -> str:
n = len(senate)
radiant = deque()
dire = deque()
# Separate senators by party
for i, party in enumerate(senate):
if party == 'R':
radiant.append(i)
else:
dire.append(i)
# Simulate the voting process
while radiant and dire:
r_index = radiant.popleft()
d_index = dire.popleft()
# The senator with lower index bans the other
# The winning senator gets to vote again in the next round
if r_index < d_index:
radiant.append(r_index + n)
else:
dire.append(d_index + n)
return "Radiant" if radiant else "Dire"
Alternative Solution (Using Two Pointers):
class Solution:
def predictPartyVictory(self, senate: str) -> str:
n = len(senate)
banned = [False] * n
r_count = d_count = 0
r_bans = d_bans = 0
# Count initial senators
for s in senate:
if s == 'R':
r_count += 1
else:
d_count += 1
while r_count > 0 and d_count > 0:
for i in range(n):
if banned[i]:
continue
if senate[i] == 'R':
if d_bans > 0:
d_bans -= 1
banned[i] = True
r_count -= 1
else:
r_bans += 1
else:
if r_bans > 0:
r_bans -= 1
banned[i] = True
d_count -= 1
else:
d_bans += 1
return "Radiant" if r_count > 0 else "Dire"
Implementation Notes:
- First solution uses queues for O(n) space and simpler logic
- Second solution uses array marking for O(n) space but more complex logic
- Both solutions have O(n) time complexity
Java Solution
class Solution {
public String predictPartyVictory(String senate) {
int n = senate.length();
Queue radiant = new LinkedList<>();
Queue dire = new LinkedList<>();
// Separate senators by party
for (int i = 0; i < n; i++) {
if (senate.charAt(i) == 'R') {
radiant.offer(i);
} else {
dire.offer(i);
}
}
// Simulate the voting process
while (!radiant.isEmpty() && !dire.isEmpty()) {
int r_index = radiant.poll();
int d_index = dire.poll();
// The senator with lower index bans the other
// The winning senator gets to vote again in the next round
if (r_index < d_index) {
radiant.offer(r_index + n);
} else {
dire.offer(d_index + n);
}
}
return radiant.isEmpty() ? "Dire" : "Radiant";
}
}
C++ Solution
class Solution {
public:
string predictPartyVictory(string senate) {
int n = senate.length();
queue radiant, dire;
// Separate senators by party
for (int i = 0; i < n; i++) {
if (senate[i] == 'R') {
radiant.push(i);
} else {
dire.push(i);
}
}
// Simulate the voting process
while (!radiant.empty() && !dire.empty()) {
int r_index = radiant.front();
int d_index = dire.front();
radiant.pop();
dire.pop();
// The senator with lower index bans the other
// The winning senator gets to vote again in the next round
if (r_index < d_index) {
radiant.push(r_index + n);
} else {
dire.push(d_index + n);
}
}
return radiant.empty() ? "Dire" : "Radiant";
}
};
JavaScript Solution
/**
* @param {string} senate
* @return {string}
*/
var predictPartyVictory = function(senate) {
const n = senate.length;
const radiant = [];
const dire = [];
// Separate senators by party
for (let i = 0; i < n; i++) {
if (senate[i] === 'R') {
radiant.push(i);
} else {
dire.push(i);
}
}
// Simulate the voting process
while (radiant.length > 0 && dire.length > 0) {
const r_index = radiant.shift();
const d_index = dire.shift();
// The senator with lower index bans the other
// The winning senator gets to vote again in the next round
if (r_index < d_index) {
radiant.push(r_index + n);
} else {
dire.push(d_index + n);
}
}
return radiant.length > 0 ? "Radiant" : "Dire";
};
C# Solution
public class Solution {
public string PredictPartyVictory(string senate) {
int n = senate.Length;
Queue radiant = new Queue();
Queue dire = new Queue();
// Separate senators by party
for (int i = 0; i < n; i++) {
if (senate[i] == 'R') {
radiant.Enqueue(i);
} else {
dire.Enqueue(i);
}
}
// Simulate the voting process
while (radiant.Count > 0 && dire.Count > 0) {
int r_index = radiant.Dequeue();
int d_index = dire.Dequeue();
// The senator with lower index bans the other
// The winning senator gets to vote again in the next round
if (r_index < d_index) {
radiant.Enqueue(r_index + n);
} else {
dire.Enqueue(d_index + n);
}
}
return radiant.Count > 0 ? "Radiant" : "Dire";
}
}
Alternative Solution (Using LINQ):
public class Solution {
public string PredictPartyVictory(string senate) {
var radiant = senate.Select((c, i) => (c, i))
.Where(x => x.c == 'R')
.Select(x => x.i)
.ToQueue();
var dire = senate.Select((c, i) => (c, i))
.Where(x => x.c == 'D')
.Select(x => x.i)
.ToQueue();
int n = senate.Length;
while (radiant.Count > 0 && dire.Count > 0) {
if (radiant.Dequeue() < dire.Dequeue()) {
radiant.Enqueue(radiant.Last() + n);
} else {
dire.Enqueue(dire.Last() + n);
}
}
return radiant.Count > 0 ? "Radiant" : "Dire";
}
}
public static class Extensions {
public static Queue ToQueue(this IEnumerable source) {
return new Queue(source);
}
}
Implementation Notes:
- First solution uses traditional queue approach
- Second solution uses LINQ for more functional style
- Both solutions have same time complexity of O(n)