Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a594bbe

Browse files
committed
Add solution #649
1 parent 0fe8aaf commit a594bbe

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

0649-dota2-senate.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
223223
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|
224224
648|[Replace Words](./0648-replace-words.js)|Medium|
225+
649|[Dota2 Senate](./0649-dota2-senate.js)|Medium|
225226
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|
226227
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
227228
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|

0 commit comments

Comments
 (0)