-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
40 lines (35 loc) · 906 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
class StackOfPlates {
public:
StackOfPlates(int cap) {
this->cap = cap;
}
void push(int val) {
if (!cap) return;
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
stk[stk.size() - 1].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:
vector<stack<int>> stk;
int cap;
};
/**
* 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);
*/