|
| 1 | +# Time: O(n) |
| 2 | +# Space: O(n) |
| 3 | + |
| 4 | +# In the world of Dota2, there are two parties: the Radiant and the Dire. |
| 5 | +# |
| 6 | +# The Dota2 senate consists of senators coming from two parties. |
| 7 | +# Now the senate wants to make a decision about a change in the Dota2 game. |
| 8 | +# The voting for this change is a round-based procedure. |
| 9 | +# In each round, each senator can exercise one of the two rights: |
| 10 | +# |
| 11 | +# Ban one senator's right: |
| 12 | +# A senator can make another senator lose all his rights in this and all the following rounds. |
| 13 | +# Announce the victory: |
| 14 | +# If this senator found the senators who still have rights to vote are all from the same party, |
| 15 | +# he can announce the victory and make the decision about the change in the game. |
| 16 | +# |
| 17 | +# Given a string representing each senator's party belonging. |
| 18 | +# The character 'R' and 'D' represent the Radiant party and the Dire party respectively. |
| 19 | +# Then if there are n senators, the size of the given string will be n. |
| 20 | +# |
| 21 | +# The round-based procedure starts from the first senator to the last senator in the given order. |
| 22 | +# This procedure will last until the end of voting. All the senators |
| 23 | +# who have lost their rights will be skipped during the procedure. |
| 24 | +# |
| 25 | +# Suppose every senator is smart enough and will play the best strategy for his own party, |
| 26 | +# you need to predict which party will finally announce the victory and make the change in the Dota2 game. |
| 27 | +# The output should be Radiant or Dire. |
| 28 | +# |
| 29 | +# Example 1: |
| 30 | +# Input: "RD" |
| 31 | +# Output: "Radiant" |
| 32 | +# Explanation: The first senator comes from Radiant and he can just ban the next senator's right in the round 1. |
| 33 | +# And the second senator can't exercise any rights any more since his right has been banned. |
| 34 | +# And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote. |
| 35 | +# Example 2: |
| 36 | +# Input: "RDD" |
| 37 | +# Output: "Dire" |
| 38 | +# Explanation: |
| 39 | +# The first senator comes from Radiant and he can just ban the next senator's right in the round 1. |
| 40 | +# And the second senator can't exercise any rights anymore since his right has been banned. |
| 41 | +# And the third senator comes from Dire and he can ban the first senator's right in the round 1. |
| 42 | +# And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote. |
| 43 | +# Note: |
| 44 | +# The length of the given string will in the range [1, 10,000]. |
| 45 | + |
| 46 | +class Solution(object): |
| 47 | + def predictPartyVictory(self, senate): |
| 48 | + """ |
| 49 | + :type senate: str |
| 50 | + :rtype: str |
| 51 | + """ |
| 52 | + n = len(senate) |
| 53 | + radiant, dire = collections.deque(), collections.deque() |
| 54 | + for i, c in enumerate(senate): |
| 55 | + if c == 'R': |
| 56 | + radiant.append(i) |
| 57 | + else: |
| 58 | + dire.append(i) |
| 59 | + while radiant and dire: |
| 60 | + r_idx, d_idx = radiant.popleft(), dire.popleft() |
| 61 | + if r_idx < d_idx: |
| 62 | + radiant.append(r_idx+n) |
| 63 | + else: |
| 64 | + dire.append(d_idx+n) |
| 65 | + return "Radiant" if len(radiant) > len(dire) else "Dire" |
0 commit comments