-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
44 lines (39 loc) · 936 Bytes
/
Solution.cpp
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
42
43
44
class StackOfPlates {
public:
StackOfPlates(int cap) {
this->cap = cap;
}
void push(int val) {
if (!cap) {
return;
}
if (stk.empty() || stk.back().size() >= cap) {
stk.emplace_back(stack<int>());
}
stk.back().push(val);
}
int pop() {
return popAt(stk.size() - 1);
}
int popAt(int index) {
int ans = -1;
if (index >= 0 && index < stk.size()) {
ans = stk[index].top();
stk[index].pop();
if (stk[index].empty()) {
stk.erase(stk.begin() + index);
}
}
return ans;
}
private:
int cap;
vector<stack<int>> stk;
};
/**
* 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);
*/