|
| 1 | +# Time: O(1) |
| 2 | +# Space: O(n) |
| 3 | + |
| 4 | +# Implement FreqStack, |
| 5 | +# a class which simulates the operation of a stack-like data structure. |
| 6 | +# |
| 7 | +# FreqStack has two functions: |
| 8 | +# |
| 9 | +# push(int x), which pushes an integer x onto the stack. |
| 10 | +# pop(), which removes and returns the most frequent element in the stack. |
| 11 | +# If there is a tie for most frequent element, |
| 12 | +# the element closest to the top of the stack is removed and returned. |
| 13 | +# |
| 14 | +# Example 1: |
| 15 | +# |
| 16 | +# Input: |
| 17 | +# ["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"], |
| 18 | +# [[],[5],[7],[5],[7],[4],[5],[],[],[],[]] |
| 19 | +# Output: [null,null,null,null,null,null,null,5,7,5,4] |
| 20 | +# Explanation: |
| 21 | +# After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top. Then: |
| 22 | +# |
| 23 | +# pop() -> returns 5, as 5 is the most frequent. |
| 24 | +# The stack becomes [5,7,5,7,4]. |
| 25 | +# |
| 26 | +# pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top. |
| 27 | +# The stack becomes [5,7,5,4]. |
| 28 | +# |
| 29 | +# pop() -> returns 5. |
| 30 | +# The stack becomes [5,7,4]. |
| 31 | +# |
| 32 | +# pop() -> returns 4. |
| 33 | +# The stack becomes [5,7]. |
| 34 | +# |
| 35 | +# Note: |
| 36 | +# - Calls to FreqStack.push(int x) will be such that 0 <= x <= 10^9. |
| 37 | +# - It is guaranteed that FreqStack.pop() won't be called if the stack has zero elements. |
| 38 | +# - The total number of FreqStack.push calls will not exceed 10000 in a single test case. |
| 39 | +# - The total number of FreqStack.pop calls will not exceed 10000 in a single test case. |
| 40 | +# - The total number of FreqStack.push and |
| 41 | +# FreqStack.pop calls will not exceed 150000 across all test cases. |
| 42 | + |
| 43 | +import collections |
| 44 | + |
| 45 | + |
| 46 | +class FreqStack(object): |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + self.__freq = collections.Counter() |
| 50 | + self.__group = collections.defaultdict(list) |
| 51 | + self.__maxfreq = 0 |
| 52 | + |
| 53 | + def push(self, x): |
| 54 | + """ |
| 55 | + :type x: int |
| 56 | + :rtype: void |
| 57 | + """ |
| 58 | + self.__freq[x] += 1 |
| 59 | + if self.__freq[x] > self.__maxfreq: |
| 60 | + self.__maxfreq = self.__freq[x] |
| 61 | + self.__group[self.__freq[x]].append(x) |
| 62 | + |
| 63 | + |
| 64 | + def pop(self): |
| 65 | + """ |
| 66 | + :rtype: int |
| 67 | + """ |
| 68 | + x = self.__group[self.__maxfreq].pop() |
| 69 | + if not self.__group[self.__maxfreq]: |
| 70 | + self.__group.pop(self.__maxfreq) |
| 71 | + self.__maxfreq -= 1 |
| 72 | + self.__freq[x] -= 1 |
| 73 | + if not self.__freq[x]: |
| 74 | + self.__freq.pop(x) |
| 75 | + return x |
| 76 | + |
| 77 | + |
| 78 | +# Your FreqStack object will be instantiated and called as such: |
| 79 | +# obj = FreqStack() |
| 80 | +# obj.push(x) |
| 81 | +# param_2 = obj.pop() |
0 commit comments