-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution649.cs
50 lines (45 loc) · 1.51 KB
/
Solution649.cs
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
43
44
45
46
47
48
49
50
namespace LeetCode.Solutions;
public class Solution649
{
/// <summary>
/// 649. Dota2 Senate - Medium
/// <a href="https://leetcode.com/problems/dota2-senate">See the problem</a>
/// </summary>
public string PredictPartyVictory(string senate)
{
var radiantQueue = new Queue<int>();
var direQueue = new Queue<int>();
var n = senate.Length;
// Initialize the queues with the index of each senator
for (var i = 0; i < n; i++)
{
if (senate[i] == 'R')
{
radiantQueue.Enqueue(i);
}
else
{
direQueue.Enqueue(i);
}
}
// Simulate the rounds
while (radiantQueue.Count > 0 && direQueue.Count > 0)
{
var radiantIndex = radiantQueue.Dequeue();
var direIndex = direQueue.Dequeue();
// Whoever has the smaller index acts first and bans the other party's senator
if (radiantIndex < direIndex)
{
// Radiant bans Dire, move Radiant to the back of the queue for the next round
radiantQueue.Enqueue(radiantIndex + n);
}
else
{
// Dire bans Radiant, move Dire to the back of the queue for the next round
direQueue.Enqueue(direIndex + n);
}
}
// If one of the queues is empty, the other party wins
return radiantQueue.Count > 0 ? "Radiant" : "Dire";
}
}