0895. Maximum Frequency Stack
https://leetcode.com/problems/maximum-frequency-stack
Description
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 integerval
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 topush
andpop
.It is guaranteed that there will be at least one element in the stack before calling
pop
.
ac
Last updated