Dota2 Senate - Problem

In the world of Dota2, there are two competing parties: the Radiant and the Dire. The Dota2 senate consists of senators from both parties who must decide on a game change through a strategic voting process.

The voting follows a round-based procedure where senators take turns in order. Each senator can:

  • Ban a senator: Remove another senator's voting rights permanently
  • Announce victory: If all remaining senators are from their party

Given a string senate where 'R' represents Radiant and 'D' represents Dire, determine which party will win. All senators play optimally for their party.

Example: senate = "RD" โ†’ "Radiant" (R bans D, then announces victory)

Input & Output

example_1.py โ€” Basic Case
$ Input: senate = "RD"
โ€บ Output: "Radiant"
๐Ÿ’ก Note: Senator R (position 0) acts first and bans senator D (position 1). Now only Radiant senators remain, so Radiant wins.
example_2.py โ€” Multiple Rounds
$ Input: senate = "RDD"
โ€บ Output: "Dire"
๐Ÿ’ก Note: Round 1: R bans first D, second D bans R. Round 2: Only the remaining D can act, so Dire wins.
example_3.py โ€” Complex Strategy
$ Input: senate = "DRDRDR"
โ€บ Output: "Dire"
๐Ÿ’ก Note: Each D bans the next R in sequence. Since D acts first each time, Dire systematically eliminates all Radiant senators.

Visualization

Tap to expand
Dota2 Senate: Strategic Voting SimulationTwo-Queue Approach VisualizationRadiant Senators (R)024Dire Senators (D)135Battle Phase1. Compare positions2. Earlier senator wins3. Winner rejoins queue4. Repeat until one winsRvsDOptimal StrategyKey Insight: Ban Next Opponent in LineEach senator bans their closest future opponentGreedy strategy leads to optimal outcomeTime: O(n) | Space: O(n) | Approach: Queue + Greedy
Understanding the Visualization
1
Setup Queues
Separate senators by party and track their original positions
2
Strategic Banning
Each senator bans their nearest opponent to maximize party advantage
3
Queue Rotation
Winning senators go to back of line for next round
4
Victory Condition
Game ends when only one party has senators remaining
Key Takeaway
๐ŸŽฏ Key Insight: Each senator should ban the next opponent in line to maximize their party's chances. Using queues makes this strategy efficient with O(n) complexity.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each senator is processed exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Two queues to store senator indices

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค senate.length โ‰ค 104
  • senate[i] is either 'R' or 'D'
  • At least one senator from each party must be present initially
Asked in
Amazon 12 Microsoft 8 Google 6 Meta 4
42.8K Views
Medium Frequency
~25 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen