forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximum Frequency Stack.cpp
More file actions
54 lines (42 loc) · 933 Bytes
/
Maximum Frequency Stack.cpp
File metadata and controls
54 lines (42 loc) · 933 Bytes
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
class FreqStack {
public:
unordered_map<int,int> ump;
unordered_map<int,stack<int>> ump_st;
int cap=1;
FreqStack() {
ump.clear();
ump_st.clear();
}
void push(int val) {
//increasing the count
if(ump.find(val)!=ump.end())
{
ump[val]++;
}
else
{
ump[val]=1;
}
//update the highest level
if(cap<ump[val])
{
cap = ump[val];
}
//push the elements in the stack where it belongs as per height
ump_st[ump[val]].push(val);
}
int pop() {
int val = ump_st[cap].top();
ump_st[cap].pop();
if(ump_st[cap].size()==0)
{
cap--;
}
ump[val]--;
if(ump[val]==0)
{
ump.erase(val);
}
return val;
}
};