class RecentCounter {
Deque<Integer> queue;
public RecentCounter() {
queue = new ArrayDeque<>();
}
public int ping(int t) {
while(!queue.isEmpty() && t - queue.peekFirst() > 3000) {
queue.removeFirst();
}
queue.addLast(t);
return queue.size();
}
}
思路:将不同阵营放到不同队列里面去,然后依次比较队首。
class Solution {
public String predictPartyVictory(String senate) {
int size = senate.length();
Deque<Integer> dd = new ArrayDeque<>();
Deque<Integer> dr = new ArrayDeque<>();
char[] chars = senate.toCharArray();
for(int i = 0; i < size; i++) {
if(chars[i] == 'D') dd.addLast(i);
else dr.addLast(i);
}
while(!dd.isEmpty() && !dr.isEmpty()) {
int ld = dd.peekFirst(), lr = dr.peekFirst();
if(ld < lr) {
dd.addLast(ld + size);
}else {
dr.addLast(lr + size);
}
dr.removeFirst();
dd.removeFirst();
}
if(dd.isEmpty()) return "Radiant";
return "Dire";
}
}