PIXELBANKv9.1.0
Menu

Design a stack-like data structure that pops the most frequent element. If there's a tie, pop the one closest to the top.

Support push(val) and pop(). Output results of pop operations.

Example:

Input:
push,5;push,7;push,5;push,7;push,4;push,5;pop;pop;pop;pop
Output:
5
7
5
4
Reasoning:
  • The stack is initially empty. We push elements 5, 7, 5, 7, 4, 5 onto the stack. The frequency of each element is: 5 (33 times), 7 (22 times), 4 (11 time).
  • When the first pop operation is performed, the most frequent element is 5, which is popped from the stack. The stack now contains 5, 7, 5, 7, 4.
  • The next pop operation again removes a 5, as it is still the most frequent element, leaving 7, 5, 7, 4 in the stack.
  • The third pop operation removes a 7, as it is now the most frequent element (tied with the remaining 5, but there are two 7s and the top one is popped), leaving 5, 7, 4 in the stack.
  • The final pop operation removes a 7 (the most frequent element, tied with 5, and the one closest to the top is not a 5), but since the output only shows the first pop result as 5, the subsequent results are 7, 5, and 4.

Constraints:

  • 0 <= val <= 10^9
  • At most 2 * 10^4 calls
solution.py

Test Results

0/0
Run code to see test results.