Maximum Frequency Stack
Problem statement
Process operations on a frequency stack.
pushadds its value.popremoves and returns a value with the greatest current frequency. Among tied values, remove the one pushed most recently.
The value paired with a pop is ignored. Return the popped values in operation order.
Function
processFrequencyStack(operations: String[], values: int[]) → int[]Examples
Example 1
operations = ["push","push","push","push","push","push","pop","pop","pop","pop"]values = [5,7,5,7,4,5,0,0,0,0]return = [5,7,5,4]The first pop chooses frequency 3; later ties use push recency.
Example 2
operations = ["push","push","pop","pop"]values = [1,2,0,0]return = [2,1]Both values initially have frequency one, so 2 leaves first.
Example 3
operations = ["push","push","push","pop"]values = [9,9,3,0]return = [9]Frequency two beats the more recent frequency-one value.
Constraints
1 <= operations.length == values.length <= 20000.- Each operation is
pushorpop. 0 <= values[i] <= 10^9for pushes.- Every pop occurs while the structure is nonempty.