Stack that pops the most frequent value
Write a class FrequencyStack of integers.
push(x)putsxon top of the stack.pop()finds the value with the most copies in the stack, removes its copy nearest the top and returns it. If several values tie for the most copies, it picks the one whose highest copy is nearest the top.
Values may be negative. pop is never called on an empty stack. Both operations must be O(1) on average.
The tests call the provided run_stack(ops): it applies each operation in order and returns the list of results (None for push, shown as null).
Besides the tests, one large input (7,500 pushes, 2,500 pops) with a ~380 ms limit (scaled to your device's speed): brute force won't pass.
Examples
run_stack([["push",5],["push",7],["push",5],["push",7],["push",4],["push",5],["pop"],["pop"],["pop"],["pop"]])→[null,null,null,null,null,null,5,7,5,4]run_stack([["push",1],["push",2],["push",3],["pop"],["pop"],["push",2],["pop"]])→[null,null,null,3,2,null,2]