-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
38 lines (35 loc) · 955 Bytes
/
Solution.java
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
class Solution {
public String predictPartyVictory(String senate) {
int rCount = 0, dCount = 0;
int rBan = 0, dBan = 0;
Queue<Character> q = new LinkedList<Character>();
for (char c : senate.toCharArray()) {
if (c == 'R') {
++rCount;
} else {
++dCount;
}
q.add(c);
}
while (rCount > 0 && dCount > 0) {
char c = q.poll();
if (c == 'R') {
if (dBan > 0) {
--dBan;
--rCount;
continue;
}
++rBan;
} else { // c == 'D'
if (rBan > 0) {
--rBan;
--dCount;
continue;
}
++dBan;
}
q.add(c);
}
return rCount > 0 ? "Radiant" : "Dire";
}
}