-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0649-dota2-senate.js
More file actions
42 lines (36 loc) · 1.22 KB
/
0649-dota2-senate.js
File metadata and controls
42 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Dota2 Senate
* Time Complexity: O(N^2)
* Space Complexity: O(N)
*/
var predictPartyVictory = function (senate) {
const senateLength = senate.length;
const radiantIndicesQueue = [];
const direIndicesQueue = [];
for (
let currentSenatorPosition = 0;
currentSenatorPosition < senateLength;
currentSenatorPosition++
) {
const partyIdentifier = senate[currentSenatorPosition];
if (partyIdentifier === "R") {
radiantIndicesQueue.push(currentSenatorPosition);
} else {
direIndicesQueue.push(currentSenatorPosition);
}
}
let radiantPartyHasMembers = radiantIndicesQueue.length > 0;
let direPartyHasMembers = direIndicesQueue.length > 0;
while (radiantPartyHasMembers && direPartyHasMembers) {
const radiantSenatorTurn = radiantIndicesQueue.shift();
const direSenatorTurn = direIndicesQueue.shift();
if (radiantSenatorTurn < direSenatorTurn) {
radiantIndicesQueue.push(radiantSenatorTurn + senateLength);
} else {
direIndicesQueue.push(direSenatorTurn + senateLength);
}
radiantPartyHasMembers = radiantIndicesQueue.length > 0;
direPartyHasMembers = direIndicesQueue.length > 0;
}
return radiantPartyHasMembers ? "Radiant" : "Dire";
};