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 ban each other’s members. The Dota2 game has a simple rule for banning:
Given a string senate
representing each senator’s party, determine which party will eventually ban the other party’s senators and win. The output should be “Radiant” if the Radiant party will win and “Dire” if the Dire party will win.
We’ll use a queue-based approach to solve the problem. The main idea is to simulate the banning process, where each senator gets their turn, and each time one senator bans an opposing senator, that senator is effectively removed from the competition. The queue helps in maintaining the circular order:
senate
string.from collections import deque
def predictPartyVictory(senate: str) -> str:
radiant = deque()
dire = deque()
for i, s in enumerate(senate):
if s == 'R':
radiant.append(i)
else:
dire.append(i)
n = len(senate)
while radiant and dire:
r_idx = radiant.popleft()
d_idx = dire.popleft()
if r_idx < d_idx:
radiant.append(r_idx + n)
else:
dire.append(d_idx + n)
return "Radiant" if radiant else "Dire"
# Example usage:
senate = "RDD"
print(predictPartyVictory(senate)) # Output: "Dire"
senate
string.This complexity is efficient and feasible for typical input sizes constrained by LeetCode.
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?