forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
41 lines (36 loc) · 975 Bytes
/
Solution.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
34
35
36
37
38
39
40
41
class StackOfPlates {
private List<Deque<Integer>> stk = new ArrayList<>();
private int cap;
public StackOfPlates(int cap) {
this.cap = cap;
}
public void push(int val) {
if (cap == 0) {
return;
}
if (stk.isEmpty() || stk.get(stk.size() - 1).size() >= cap) {
stk.add(new ArrayDeque<>());
}
stk.get(stk.size() - 1).push(val);
}
public int pop() {
return popAt(stk.size() - 1);
}
public int popAt(int index) {
int ans = -1;
if (index >= 0 && index < stk.size()) {
ans = stk.get(index).pop();
if (stk.get(index).isEmpty()) {
stk.remove(index);
}
}
return ans;
}
}
/**
* Your StackOfPlates object will be instantiated and called as such:
* StackOfPlates obj = new StackOfPlates(cap);
* obj.push(val);
* int param_2 = obj.pop();
* int param_3 = obj.popAt(index);
*/