堆盘子00
class StackOfPlates {
int cap;
List<Deque> list;
public StackOfPlates(int cap) {
this.cap = cap;
list = new ArrayList<>();
}
public void push(int val) {
if (cap == 0) {
return;
}
if (list.isEmpty() || list.get(list.size() - 1).size() >= cap) {
Deque<Integer> deque = new ArrayDeque<>();
deque.push(val);
list.add(deque);
} else {
Deque<Integer> deque = list.get(list.size() - 1);
deque.push(val);
}
}
public int pop() {
if (list.isEmpty()) {
return -1;
}
Deque<Integer> deque = list.get(list.size() - 1);
int res = deque.pop();
if (deque.isEmpty()) {
list.remove(list.size() - 1);
}
return res;
}
public int popAt(int index) {
if (index >= list.size()) {
return -1;
}
Deque<Integer> deque = list.get(index);
int res = deque.pop();
if (deque.isEmpty()) {
list.remove(index);
}
return res;
}
}