forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_649.java
33 lines (30 loc) · 1.12 KB
/
_649.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
package com.fishercoder.solutions;
import java.util.LinkedList;
import java.util.Queue;
public class _649 {
public static class Solution1 {
public String predictPartyVictory(String senate) {
Queue<Integer> radiantQ = new LinkedList<>();
Queue<Integer> direQ = new LinkedList<>();
int len = senate.length();
for (int i = 0; i < len; i++) {
if (senate.charAt(i) == 'R') {
radiantQ.offer(i);
} else {
direQ.offer(i);
}
}
while (!radiantQ.isEmpty() && !direQ.isEmpty()) {
int radiantIndex = radiantQ.poll();
int direIndex = direQ.poll();
if (radiantIndex < direIndex) {
/**Radiant will ban Dire in this case, so we'll add radiant index back to the queue plus n*/
radiantQ.offer(radiantIndex + len);
} else {
direQ.offer(direIndex + len);
}
}
return radiantQ.isEmpty() ? "Dire" : "Radiant";
}
}
}