|
| 1 | +class Solution: |
| 2 | + """Base class for all LeetCode Problems.""" |
| 3 | + |
| 4 | + def predictPartyVictory(self, senate: str) -> str: |
| 5 | + """ |
| 6 | + In the world of Dota2, there are two parties: the Radiant and the Dire. |
| 7 | +
|
| 8 | + The Dota2 senate consists of senators coming from two parties. Now the Senate |
| 9 | + wants to decide on a change in the Dota2 game. The voting for this change is a |
| 10 | + round-based procedure. In each round, each senator can exercise one of the two |
| 11 | + rights: |
| 12 | + - Ban one senator's right: A senator can make another senator lose all his |
| 13 | + rights in this and all the following rounds. |
| 14 | + - Announce the victory: If this senator found the senators who still have |
| 15 | + rights to vote are all from the same party, he can announce the victory and |
| 16 | + decide on the change in the game. |
| 17 | +
|
| 18 | + Given a string senate representing each senator's party belonging. The |
| 19 | + character 'R' and 'D' represent the Radiant party and the Dire party. Then if |
| 20 | + there are n senators, the size of the given string will be n. |
| 21 | +
|
| 22 | + The round-based procedure starts from the first senator to the last senator in |
| 23 | + the given order. This procedure will last until the end of voting. All the |
| 24 | + senators who have lost their rights will be skipped during the procedure. |
| 25 | +
|
| 26 | + Suppose every senator is smart enough and will play the best strategy for his |
| 27 | + own party. Predict which party will finally announce the victory and change the |
| 28 | + Dota2 game. The output should be "Radiant" or "Dire". |
| 29 | + """ |
| 30 | + queue = list(senate) |
| 31 | + while len(queue) > 1: |
| 32 | + senator = queue.pop(0) |
| 33 | + another = "D" if senator == "R" else "R" |
| 34 | + try: |
| 35 | + index = queue.index(another) |
| 36 | + except ValueError: |
| 37 | + break |
| 38 | + queue.pop(index) |
| 39 | + queue.append(senator) |
| 40 | + return "Radiant" if queue.pop(0) == "R" else "Dire" |
0 commit comments