如果cooldown很小,可认为是常数,如何做到,O(n)时间,O(1)空间?
public String schedule11(int [] array, int recover){
Queue<Integer> queue = new LinkedList<>();
StringBuilder sb = new StringBuilder();
int total = 0;
int i = 0;
while(i<array.length){
if(queue.isEmpty() || !queue.contains(array[i])){
queue.add(array[i]);
i++;
}
else{
queue.add(null);
}
total++;
if(queue.size()>recover){
//queue.poll();
if (queue.peek() != null) {
sb.append(queue.poll()+",");
}
else {
queue.poll();
sb.append("_,");
}
}
}
while (!queue.isEmpty()) {
if (queue.peek() != null) {
sb.append(queue.poll()+",");
}
else {
queue.poll();
sb.append("_,");
}
}
System.out.println("time:" + total + " strLen:" + sb.length());
return sb.toString();
}