Dota2 Senate

Medium · Stack & Strings · String · Greedy · Queue

Problem

A senate has senators from two parties, Radiant and Dire, given as a string senate where 'R' and 'D' mark each senator's party, in seating order.

Voting runs in rounds. Within a round, senators act in seating order, and each senator who still has rights does one of two things:

Banned senators are skipped. Every senator plays as well as possible for their party. Return "Radiant" or "Dire": the party that wins.

Examples

Input: senate = "RDD"
Output: "Dire"

R bans the first D, but the second D then bans R.

Input: senate = "DRR"
Output: "Radiant"

D bans one R; the other R bans D, and only Radiant is left.

Input: senate = "RRDDD"
Output: "Radiant"

Both Rs act first and ban two Ds; the last D bans one R, and the surviving R bans it in the next round.

Constraints

Approaches

Follow-up

Each comparison bans one senator, so the queues finish in O(n) steps. A related question is how many senators are left at the end, or in which order they were banned; the same queues answer both. With three or more parties, "ban the next opponent" is no longer clearly optimal, because helping one rival can hurt you more than another.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…