The Dota2 match consists of two parties: the Radiant and the Dire. The Dota2 senate consists of senators from both parties who vote on a proposed change to the game. Given a string senate
representing each senator in the senate where:
The voting process follows these rules:
Your task is to determine which party will remain after the voting process ends. Return the string “Radiant” if the Radiant party wins and “Dire” if the Dire party wins.
import java.util.LinkedList;
import java.util.Queue;
public class Dota2Senate {
public String predictPartyVictory(String senate) {
Queue<Integer> radiant = new LinkedList<>();
Queue<Integer> dire = new LinkedList<>();
// Populate the queues with the indices of each senator
for (int i = 0; i < senate.length(); i++) {
if (senate.charAt(i) == 'R') {
radiant.add(i);
} else {
dire.add(i);
}
}
// Process the voting rounds
while (!radiant.isEmpty() && !dire.isEmpty()) {
int rIndex = radiant.poll();
int dIndex = dire.poll();
// Lower index senator bans the other
if (rIndex < dIndex) {
radiant.add(rIndex + senate.length());
} else {
dire.add(dIndex + senate.length());
}
}
return radiant.isEmpty() ? "Dire" : "Radiant";
}
}
Thus, the total time complexity is O(n), which is efficient for the input size constraint of 10^4.
This approach ensures each round is efficiently handled, and queues provide an intuitive and straightforward way to handle the cyclic nature of voting rounds in this problem.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?