-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdinnerPlateStacks.cpp
More file actions
54 lines (47 loc) · 1.36 KB
/
dinnerPlateStacks.cpp
File metadata and controls
54 lines (47 loc) · 1.36 KB
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
45
46
47
48
49
50
51
52
53
54
// Source: https://leetcode.com/problems/dinner-plate-stacks/
// Author: Miao Zhang
// Date: 2021-04-15
class DinnerPlates {
public:
DinnerPlates(int capacity) : cap_(capacity) {
}
void push(int val) {
int idx = index_.empty() ? stacks_.size() : *begin(index_);
if (idx == stacks_.size()) stacks_.emplace_back();
stack<int>& s = stacks_[idx];
s.push(val);
if (s.size() == cap_) {
index_.erase(idx);
} else if (s.size() == 1) {
index_.insert(idx);
}
}
int pop() {
return popAtStack(stacks_.size() - 1);
}
int popAtStack(int index) {
if (index < 0 || index >= stacks_.size() || stacks_[index].empty()) return -1;
stack<int>& s = stacks_[index];
int val = s.top();
s.pop();
if (s.size() == cap_ - 1)
index_.insert(index);
auto it = prev(end(index_));
while (stacks_.size() && stacks_.back().empty()) {
stacks_.pop_back();
index_.erase(it--);
}
return val;
}
private:
int cap_;
set<int> index_;
vector<stack<int>> stacks_;
};
/**
* Your DinnerPlates object will be instantiated and called as such:
* DinnerPlates* obj = new DinnerPlates(capacity);
* obj->push(val);
* int param_2 = obj->pop();
* int param_3 = obj->popAtStack(index);
*/