649. Dota2 Senate
Dota2 Senate (LeetCode 649) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Greedy Patterns pattern, in the Task Scheduling (Frequency Based) subpattern.
How to solve Dota2 Senate
Dota2 Senate is a Task Scheduling problem, part of Greedy Patterns. Schedule tasks with cooldown using frequency-based greedy approach.
Work towards the solution in these steps:
- Track which senators are still allowed to act rather than rebuilding the whole sequence after every ban.
- A senator can always target an opposing senator that appears later in the current circular pass; if none is available there, a senator from a later pass can be targeted.
- Use separate queues of turn positions for the two parties, ordered by position.
The reference solution runs in O(n) time and O(n) space.
The Dota2 Senate solution and editorial walks through the full approach with reference code.
Problem statement
Implement Solution.predictPartyVictory, which receives senate, a non-empty string describing senators arranged in a circle. Each character is one senator: R belongs to the Radiant party and D belongs to the Dire party. The characters' order determines the order in which senators act, starting with the first character.
When an active senator takes a turn, that senator may permanently ban one active senator from the opposing party. A banned senator loses the right to act and vote, while the senator who performed the ban remains active. After the final position in senate is processed, continue again from the first position, skipping banned senators.
Continue until every active senator belongs to the same party. Return exactly "Radiant" if Radiant remains, or exactly "Dire" if Dire remains.
Example 1:
Input: senate = "DRR" Output: "Radiant" Explanation: The first Dire senator bans one Radiant senator, but the remaining Radiant senator later bans that Dire senator. Radiant is the only party left.
Example 2:
Input: senate = "RDDR" Output: "Radiant" Explanation: The first Radiant senator removes the next Dire senator, and the later turns allow Radiant to eliminate the remaining Dire senator before losing its own active senators.
Example 3:
Input: senate = "DDRRR" Output: "Dire" Explanation: The two Dire senators can successively remove two Radiant senators, and the remaining exchanges leave Dire with an active senator while all Radiant senators are banned.
Constraints:
- 1 <= senate.length <= 10^5 - Every character in senate is either R or D. - The senators are processed in the circular order represented by senate. - A senator can ban at most one opposing senator per turn. - A banned senator cannot act or vote on any later turn.
Topics and companies
Topics: Greedy, Queue, Simulation, String.
How to practise Dota2 Senate on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.
Open Dota2 Senate in the code editor, or read the Dota2 Senate editorial for a worked solution with its approach and complexity analysis.
Where Dota2 Senate sits in the DSA pattern sheet
- Greedy Patterns pattern guide
- Task Scheduling (Frequency Based) subpattern
- The full DSA patterns sheet
Problems related to Dota2 Senate
Other problems that use the same Task Scheduling (Frequency Based) and Greedy Patterns techniques:
- Distant Barcodes — Medium, same subpattern
- Task Scheduler — Medium, same subpattern
- Assign Cookies — Easy, same pattern
- Candy — Hard, same pattern
- Car Pooling — Medium, same pattern
- Divide Intervals Into Minimum Number of Groups — Medium, same pattern
Dota2 Senate FAQ
What is the LeetCode number of Dota2 Senate?
Dota2 Senate is LeetCode problem 649, rated Medium.
What pattern is used to solve Dota2 Senate?
Dota2 Senate is a Task Scheduling problem. It belongs to the Greedy Patterns pattern, in the Task Scheduling (Frequency Based) subpattern.
What is the time complexity of Dota2 Senate?
The reference solution runs in O(n) time and O(n) space.