-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotaSenate.java
More file actions
56 lines (41 loc) · 1.52 KB
/
Copy pathDotaSenate.java
File metadata and controls
56 lines (41 loc) · 1.52 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package LeetcodePractice;
import java.util.LinkedList;
import java.util.Queue;
public class DotaSenate {
public static String predictPartyVictory(String senate) {
Queue<Integer> radiant = new LinkedList<>();
Queue<Integer> dire = new LinkedList<>();
int n = senate.length();
for (int i = 0; i < n; i++) {
if (senate.charAt(i) == 'R') {
radiant.offer(i);
} else {
dire.offer(i);
}
}
// Simulate the rounds
while (!radiant.isEmpty() && !dire.isEmpty()) {
int rIndex = radiant.poll();
int dIndex = dire.poll();
if (rIndex < dIndex) {
radiant.offer(rIndex + n);
} else {
dire.offer(dIndex + n);
}
}
return radiant.isEmpty() ? "Dire" : "Radiant";
}
public static void main(String[] args) {
String senate1 = "RD";
System.out.println("Input: " + senate1);
System.out.println("Winner: " + predictPartyVictory(senate1));
System.out.println();
String senate2 = "RDD";
System.out.println("Input: " + senate2);
System.out.println("Winner: " + predictPartyVictory(senate2));
System.out.println();
String senate3 = "RRDDD";
System.out.println("Input: " + senate3);
System.out.println("Winner: " + predictPartyVictory(senate3));
}
}