Frequency Stack
Implement a frequency stack with the following methods:
FrequencyStack()constructs a new instance of a frequency stackappend(int val)appendsvalto the stackpop()pops and returns the most frequent element in the stack. If there’s more than one most frequent element, the one that’s closer to the top of the stack should be popped first.
You can assume that for pop, the stack is non-empty when they are called.
Constraints
n ≤ 100,000wherenis the number of methods that will be called toappendandpop
https://binarysearch.com/problems/Frequency-Stack
Examples
Example 1
Input
- methods =
['constructor', 'append', 'append', 'append', 'pop', 'pop', 'pop'] - arguments =
[[], [1], [1], [2], [], [], []]
Output
- answer =
[None, None, None, None, 1, 2, 1]
Leave a comment