-
Notifications
You must be signed in to change notification settings - Fork 1
/
1172.餐盘栈.cpp
108 lines (90 loc) · 2.21 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "s.h"
/*
* @lc app=leetcode.cn id=1172 lang=cpp
*
* [1172] 餐盘栈
*/
// @lc code=start
class DinnerPlates {
private:
vector<stack<int>> stacks;
int capacity;
int right{-1};
set<int> pq;
public:
DinnerPlates(int cap) {
capacity = cap;
}
void push(int val) {
if(pq.empty()){
if(right==-1 || stacks[right].size()==capacity){
stacks.push_back({});
right++;
stacks[right].push(val);
return;
}else{
stacks[right].push(val);
}
}else{
int index = *pq.begin();
stacks[index].push(val);
if(stacks[index].size()==capacity){
pq.erase(index);
}
}
}
int pop() {
if(right==-1){
cout << -1 << endl;
return -1;
}
int val = stacks[right].top();
stacks[right].pop();
while(right>=0 && stacks[right].empty()){
right--;
stacks.pop_back();
pq.erase(right);
}
cout << val << endl;
return val;
}
int popAtStack(int index) {
if(index==right){
int val = pop();
cout << val << endl;
return val;
}else if(index>right){
return -1;
}else{
if(stacks[index].empty()){
return -1;
}else{
int val = stacks[index].top();
stacks[index].pop();
pq.insert(index);
return val;
cout << val << endl;
}
}
return -1;
}
};
/**
* 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);
*/
// @lc code=end
int main(){
DinnerPlates dinner(1);
dinner.push(1);
dinner.push(2);
dinner.push(3);
dinner.popAtStack(0);
dinner.pop();
dinner.pop();
dinner.push(1);
dinner.push(2);
}