forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1172.cpp
35 lines (32 loc) · 923 Bytes
/
1172.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
class DinnerPlates
{
public:
DinnerPlates(int capacity) : n(capacity){}
void push(int val)
{
while (!q.empty() and q.top() < stacks.size() and stacks[q.top()].size() == n) q.pop();
if (q.empty()) q.push(stacks.size());
if (q.top() == stacks.size()) stacks.push_back({});
stacks[q.top()].push_back(val);
}
int pop()
{
while (!stacks.empty() and stacks.back().empty()) stacks.pop_back();
return popAtStack(stacks.size() - 1);
}
int popAtStack(int index)
{
if (index >= 0 and index < stacks.size() and !stacks[index].empty())
{
auto ret = stacks[index].back();
stacks[index].pop_back();
q.push(index);
return ret;
}
return -1;
}
private:
priority_queue<int, vector<int>, greater<int>> q;
int n;
vector<vector<int>> stacks;
};