649.Dota2 Senate === ###### tags: `Medium`,`String`,`Greedy`,`Queue` [649. Dota2 Senate](https://leetcode.com/problems/dota2-senate/) ### 題目描述 In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise **one** of the two rights: * **Ban one senator's right:** A senator can make another senator lose all his rights in this and all the following rounds. * **Announce the victory:** If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game. Given a string `senate` representing each senator's party belonging. The character `'R'` and `'D'` represent the Radiant party and the Dire party. Then if there are `n` senators, the size of the given string will be `n`. The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure. Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be `"Radiant"` or `"Dire"`. ### 範例 **Example 1:** ``` Input: senate = "RD" Output: "Radiant" Explanation: The first senator comes from Radiant and he can just ban the next senator's right in round 1. And the second senator can't exercise any rights anymore since his right has been banned. And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote. ``` **Example 2:** ``` Input: senate = "RDD" Output: "Dire" Explanation: The first senator comes from Radiant and he can just ban the next senator's right in round 1. And the second senator can't exercise any rights anymore since his right has been banned. And the third senator comes from Dire and he can ban the first senator's right in round 1. And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote. ``` **Constraints**: * `n` == `senate.length` * 1 <= `n` <= 10^4^ * `senate[i]` is either `'R'` or `'D'`. ### 解答 #### Javascript ```javascript= function predictPartyVictory(senate) { const queue = [...senate]; while (queue.length > 1) { const current = queue.shift(); if (current === 'B') continue; let isSame = true; for (let i = 0; i < queue.length; i++) { if (queue[i] !== current && queue[i] !== 'B') { queue[i] = 'B'; isSame = false; break; } } queue.push(current); if (isSame) break; } if (queue[queue.length - 1] === 'R') return 'Radiant'; return 'Dire'; } ``` > [name=Marsgoat][time=May 4, 2023] 看答案用兩個queue解的版本 ```javascript= function predictPartyVictory(senate) { const n = senate.length; const rQueue = []; const dQueue = []; for (let i = 0; i < n; i++) { if (senate[i] === 'R') { rQueue.push(i); } else { dQueue.push(i); } } while (rQueue.length && dQueue.length) { const rTurn = rQueue.shift(); const dTurn = dQueue.shift(); // index較小的代表是排在前面的,可以優先ban if (dTurn < rTurn) { dQueue.push(dTurn + n); } else { rQueue.push(rTurn + n); } } return rQueue.length === 0 ? 'Dire' : 'Radiant'; } ``` > 看解答學到很多,這樣就可以不用每次都往後找人ban,時間複雜度從$O(n^2)$變成$O(n)$ > 我覺得這題蠻有趣的啊,不知道為何倒讚這麼多。 > [name=Marsgoat][time=May 4, 2023] #### Python ```python= class Solution: def predictPartyVictory(self, senate: str) -> str: n = len(senate) qR = deque(i for i, senator in enumerate(senate) if senator == 'R') qD = deque(i for i, senator in enumerate(senate) if senator == 'D') while qR and qD: R_turn = qR.popleft() D_turn = qD.popleft() if R_turn < D_turn: qR.append(R_turn + n) else: qD.append(D_turn + n) return "Radiant" if qR else "Dire" ``` > 沒有想到$O(n)$解,所以看解答+1 ```python= import queue class Solution: def predictPartyVictory(self, senate: str) -> str: n = len(senate) qR = queue.Queue() for i, senator in enumerate(senate): if senator == 'R': qR.put(i) qD = queue.Queue() for i, senator in enumerate(senate): if senator == 'D': qD.put(i) while not qR.empty() and not qD.empty(): R_turn = qR.get() D_turn = qD.get() if R_turn < D_turn: qR.put(R_turn + n) else: qD.put(D_turn + n) return "Radiant" if qD.empty() else "Dire" ``` > 附帶一個 Python queue 寫法,不但要先 import queue,速度還比 deque 慢 > [name=Ron Chen][time=Thu, May 4, 2023] ### Reference [回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)