|
| 1 | +/** |
| 2 | + * 649. Dota2 Senate |
| 3 | + * https://leetcode.com/problems/dota2-senate/ |
| 4 | + * Difficulty: Medium |
| 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 wants |
| 9 | + * to decide on a change in the Dota2 game. The voting for this change is a round-based |
| 10 | + * procedure. In each round, each senator can exercise one of the two rights: |
| 11 | + * - Ban one senator's right: A senator can make another senator lose all his rights in |
| 12 | + * this and all the following rounds. |
| 13 | + * - Announce the victory: If this senator found the senators who still have rights to |
| 14 | + * vote are all from the same party, he can announce the victory and decide on the |
| 15 | + * change in the game. |
| 16 | + * |
| 17 | + * Given a string senate representing each senator's party belonging. The character 'R' |
| 18 | + * and 'D' represent the Radiant party and the Dire party. Then if there are n senators, |
| 19 | + * 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 |
| 22 | + * given order. This procedure will last until the end of voting. All the senators who |
| 23 | + * 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 |
| 26 | + * party. Predict which party will finally announce the victory and change the Dota2 |
| 27 | + * game. The output should be "Radiant" or "Dire". |
| 28 | +*/ |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {string} senate |
| 32 | + * @return {string} |
| 33 | + */ |
| 34 | +var predictPartyVictory = function(senate) { |
| 35 | + const rQueue = []; |
| 36 | + const dQueue = []; |
| 37 | + |
| 38 | + for (let i = 0; i < senate.length; i++) { |
| 39 | + if (senate[i] === 'R') rQueue.push(i); |
| 40 | + else dQueue.push(i); |
| 41 | + } |
| 42 | + |
| 43 | + while (rQueue.length && dQueue.length) { |
| 44 | + const [rIndex, dIndex] = [rQueue.shift(), dQueue.shift()]; |
| 45 | + if (rIndex < dIndex) { |
| 46 | + rQueue.push(rIndex + senate.length); |
| 47 | + } else { |
| 48 | + dQueue.push(dIndex + senate.length); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return rQueue.length > 0 ? 'Radiant' : 'Dire'; |
| 53 | +}; |
0 commit comments