Build a stack-like structure, FreqStack, that hands values back according to how often they currently appear rather than purely by insertion order. It must support two operations:
push(val): place the integer val on the stack.pop(): take out and return the value that occurs the greatest number of times among the elements currently in the stack.
Implement process_operations(operations, arguments), which drives such a structure. operations is the ordered list of operation names, and arguments holds the matching argument list for each operation ([x] for a push of the integer x, and [] for a pop). The first operation may be the constructor name "FreqStack" with an empty argument list. The returned list has one entry per operation, in order: the popped value for each pop, and null for every other operation (the constructor and each push).
Example 1:
Input:
push(5)
push(7)
push(5)
push(7)
push(4)
push(5)
pop()
pop()
pop()
pop()
Output: 5, 7, 5, 4
Explanation: After the pushes, 5 appears three times, so it comes out first; then 5 and 7 are tied at two and the 7 was pushed later, so 7 is returned; next 5 is the only value with count two; finally every remaining value appears once and 4 is closest to the top. The four values above are the results of the four pop calls; in the process_operations form each push additionally contributes a null entry to the returned list.
Example 2:
Input:
operations = ["FreqStack","push","push","push","push","pop","pop","pop"]
arguments = [[],[1],[1],[2],[2],[],[],[]]
Output:
[null,null,null,null,null,2,1,2]
Explanation: After the pushes, both 1 and 2 appear twice. The first pop returns 2, because the last 2 was pushed after the last 1. The second pop returns 1, the only value still appearing twice. Once the count-two group is exhausted, the highest count drops to one, and the next pop returns 2, since it was pushed after the remaining 1.
Example 3:
Input:
operations = ["FreqStack","push","pop"]
arguments = [[],[5],[]]
Output:
[null,null,5]
Explanation: A single push followed by a pop simply returns the pushed value.
Constraints:
q is the total number of operations.x.pop is guaranteed to be performed on a non-empty stack.push and each pop.