-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution649.java
42 lines (33 loc) · 1.13 KB
/
Solution649.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
39
40
41
42
package leetcode.queue;
import java.util.LinkedList;
import java.util.Queue;
public class Solution649 {
public static void main(String[] args) {
// Radiant
System.out.println(new Solution649().predictPartyVictory("DRRDRDRDRDDRDRDR"));
}
public String predictPartyVictory(String senate) {
Queue<Integer> rQueue = new LinkedList<>();
Queue<Integer> dQueue = new LinkedList<>();
char[] charArray = senate.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] == 'D') {
dQueue.offer(i);
} else {
rQueue.offer(i);
}
}
while (!rQueue.isEmpty() && !dQueue.isEmpty()) {
Integer r = rQueue.poll();
Integer d = dQueue.poll();
if (r < d) {
rQueue.offer(r + senate.length());
charArray[d % charArray.length] = 'x';
} else {
dQueue.offer(d + senate.length());
charArray[r % charArray.length] = 'x';
}
}
return rQueue.isEmpty() ? "Dire" : "Radiant";
}
}