A senate has senators from two parties, Radiant and Dire, written as a string senate of R and D letters. They act in turns, in the order of the string, round after round. A senator who has been banned is skipped for good.
On their turn a senator can either ban one other senator, or, if everyone still allowed to act is from their own party, declare victory. Every senator plays as well as possible for their party. Return "Radiant" or "Dire": the party that wins.
Example 1
Input: senate = "RDDRD" Output: "Dire" Why: after one round only senators 2 and 4, both Dire, are left
2 more examples and the constraints
Example 2
Input: senate = "RRDDD" Output: "Radiant" Why: the two R speak first and ban two D; the last D bans one R, the other R bans it
Example 3
Input: senate = "D" Output: "Dire" Why: a lone senator wins at once
Constraints
- 1 ≤ length of senate ≤ 10,000
- Every character is R or D
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Simulate on the stringO(n²)
- Two queues of turnsO(n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 10,000 senators | Verdict |
|---|---|---|---|
| Simulate on the string | O(n²) | Shows the greedy; slow | |
| Two queues of turns | O(n) | Best; one duel per ban |
n = number of senators.
Edge cases to test
- One senator. "D" wins straight away with no bans.
- Only one party. "RRR" is Radiant: the loop never runs.
- The minority can win. "RRDDD" is Radiant, because speaking first matters more than numbers.
- Wrapping around. The last senator may ban someone at the front of the string, as senator 4 does in Example 1.
Common mistakes
- Banning the nearest opponent in the string to the left instead of the next one to speak.
- Re-adding the winner with the same index, so it acts again in the same round.
- Deciding the winner by counting letters; the order of speaking decides it.
How to talk through it
- 1Clarify. Do the rounds wrap around in the same order? Is the string only R and D?
- 2Argue the greedy. Ban the next opponent to speak: anyone later is less urgent.
- 3Give the simulation. Search and delete in the string; O(n²).
- 4Use two queues. Fronts duel, the winner goes back with index + n. O(n) time.
- 5Test out loud. Run "D", "RRDDD" and a case that wraps around.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?