Design a stack-like structure that pops the most frequent element.
- `FreqStack()` creates an empty frequency stack.
- `void push(int val)` pushes `val` on top.
- `int pop()` removes and returns the most frequent element in the stack. If several elements tie for the highest frequency, the one closest to the top (pushed most recently) is removed and returned.
Design a stack-like structure that pops the most frequent element.
FreqStack() creates an empty frequency stack.
void push(int val) pushes val on top.
int pop() removes and returns the most frequent element in the stack. If several elements tie for the highest frequency, the one closest to the top (pushed most recently) is removed and returned.
Empty structure. freq counts each value; group[f] is a stack of values in the order they reached count f. Popping the top of group[maxfreq] gives the most frequent, most recent value.
group[f]: one stack per frequency level
maxfreq = 0
empty
a value seen 3 times sits on levels 1, 2 and 3; pop always takes the top of the highest level