LC.P649[Dota2参议院]

方法一:贪心+循环队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public String predictPartyVictory(String senate) {
int n = senate.length();
Deque<Integer> rd = new ArrayDeque<>(), dd = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (senate.charAt(i) == 'R') rd.offer(i);
else dd.offer(i);
}
while (!rd.isEmpty() && !dd.isEmpty()) {
int a = rd.poll(), b = dd.poll();
if (a < b) rd.offer(a + n);
else dd.offer(b + n);
}
return !rd.isEmpty() ? "Radiant" : "Dire";
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$