> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Dota2 Senate Python Solution with Tests

> Tested Python solution for LeetCode 649 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 649, Medium. Topics: String, Greedy, Queue. [View on LeetCode](https://leetcode.com/problems/dota2-senate/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 649   # by problem number
lcpy gen -s dota2_senate   # by problem name
```

## Problem

In the world of Dota2, there are two parties: the Radiant and the Dire.

The Dota2 senate consists of senators from both parties. Voting is a round-based procedure. In each round, each senator (in order) can exercise one right:

* **Ban one senator's right:** make another senator lose all rights in this and all following rounds.
* **Announce the victory:** if all senators who still have rights are from the same party, announce victory.

Given a string `senate` where `'R'` is Radiant and `'D'` is Dire, predict which party announces victory. Output `"Radiant"` or `"Dire"`. Every senator plays optimally for their own party.

### Examples

```
Input: senate = "RD"
Output: "Radiant"
Explanation: The first senator (Radiant) bans the next senator's right in round 1. In round 2, the first senator announces victory.
```

```
Input: senate = "RDD"
Output: "Dire"
Explanation: The first senator (Radiant) bans the second senator's right. The third senator (Dire) bans the first senator's right. In round 2, the third senator announces victory.
```

### Constraints

* n == senate.length
* 1 \<= n \<= 10^4
* senate\[i] is either 'R' or 'D'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/dota2_senate/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/dota2_senate/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(n) each senator is banned at most once
    # Space: O(n) for the queues
    def predict_party_victory(self, senate: str) -> str:
        n = len(senate)
        radiant: deque[int] = deque(i for i, ch in enumerate(senate) if ch == "R")
        dire: deque[int] = deque(i for i, ch in enumerate(senate) if ch == "D")

        while radiant and dire:
            r = radiant.popleft()
            d = dire.popleft()
            # Earlier senator bans the other; winner re-enters with a future index.
            if r < d:
                radiant.append(r + n)
            else:
                dire.append(d + n)

        return "Radiant" if radiant else "Dire"
```

## Complexity

| Time                                     | Space               |
| ---------------------------------------- | ------------------- |
| O(n) each senator is banned at most once | O(n) for the queues |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
