Skip to content

Commit 0aad6d8

Browse files
committed
added predictPartyVictory
1 parent e7a738a commit 0aad6d8

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { checkDecodeStringDBG } from "./stack/decode-string";
1+
import { dota2SenateDBG } from "./queue/predict-party-victory";
22

3-
checkDecodeStringDBG()
3+
dota2SenateDBG()

src/queue/predict-party-victory.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @problem
3+
* [649. Dota2 Senate](https://leetcode.com/problems/dota2-senate)
4+
*/
5+
function predictPartyVictory(senate: string): string {
6+
const n = senate.length;
7+
8+
const rQueue: number[] = [];
9+
const dQueue: number[] = [];
10+
11+
for (let i = 0; i < n; i++) {
12+
if (senate[i] === 'R') {
13+
rQueue.push(i);
14+
} else {
15+
dQueue.push(i);
16+
}
17+
}
18+
19+
while (rQueue.length > 0 && dQueue.length > 0) {
20+
const rTurn = rQueue.shift();
21+
const dTurn = dQueue.shift();
22+
23+
if (dTurn < rTurn) {
24+
dQueue.push(dTurn + n);
25+
} else {
26+
rQueue.push(rTurn + n);
27+
}
28+
}
29+
return rQueue.length === 0
30+
? "Dire"
31+
: "Radiant"
32+
};
33+
34+
35+
36+
export function dota2SenateDBG() {
37+
const tests = [
38+
{
39+
input: 'DDRRR',
40+
result: 'Dire'
41+
},
42+
{
43+
input: 'DRRDRDRDRDDRDRDRRDR',
44+
result: 'Radiant'
45+
},
46+
{
47+
input: 'RRDDD',
48+
result: 'Radiant'
49+
},
50+
{
51+
input: 'RRRRRDDDD',
52+
result: 'Radiant'
53+
},
54+
];
55+
56+
tests.forEach((test, index) => {
57+
const result = predictPartyVictory(test.input);
58+
const success = result === test.result;
59+
if (success) {
60+
console.log(`Test ${index} success`);
61+
} else {
62+
console.log(`Test ${index} fail`);
63+
console.log(`expected "${test.result}"`);
64+
console.log(`got "${result}"`);
65+
}
66+
});
67+
}

0 commit comments

Comments
 (0)