-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDinner Plate Stacks.cpp
57 lines (53 loc) · 1.06 KB
/
Dinner Plate Stacks.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
class DinnerPlates {
public:
map<int,stack<int>>mp;
set<int>empty;
int cap;
DinnerPlates(int capacity)
{
this->cap=capacity;
}
void push(int val)
{
if(empty.size()==0)
{
empty.insert(mp.size());
}
mp[*empty.begin()].push(val);
if(mp[*empty.begin()].size()==cap)
{
empty.erase(empty.begin());
}
}
int pop()
{
if(mp.size()==0)
{
return -1;
}
int index=mp.rbegin()->first;
int val=mp[index].top();
mp[index].pop();
empty.insert(index);
if(mp[index].size()==0)
{
mp.erase(index);
}
return val;
}
int popAtStack(int index)
{
if(mp.size()==0||mp.find(index)==mp.end())
{
return -1;
}
int val=mp[index].top();
mp[index].pop();
empty.insert(index);
if(mp[index].size()==0)
{
mp.erase(index);
}
return val;
}
};