Last updated
Was this helpful?
Last updated
Was this helpful?
https://leetcode.com/problems/maximum-frequency-stack
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
Implement the FreqStack
class:
FreqStack()
constructs an empty frequency stack.
void push(int val)
pushes an integer val
onto the top of the stack.
int pop()
removes and returns the most frequent element in the stack.
If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
Example 1:
Constraints:
0 <= val <= 109
At most 2 * 104
calls will be made to push
and pop
.
It is guaranteed that there will be at least one element in the stack before calling pop
.