Maximum Frequency Stack LeetCode solution in Python
1# Runtime: 347 ms (Top 94.76%) | Memory: 22.7 MB (Top 60.86%)2class FreqStack:34def __init__(self):5self.cnt = {}6self.maxcount = 07self.stack = {}89def push(self, val: int) -> None:10count = self.cnt.get(val, 0) + 111self.cnt[val] = count12if count > self.maxcount:13self.maxcount = count14self.stack[count] = []15self.stack[count].append(val)1617def pop(self) -> int:18res = self.stack[self.maxcount].pop()19self.cnt[res] -= 120if not self.stack[self.maxcount]:21self.maxcount -= 122return res232425# Your FreqStack object will be instantiated and called as such:26# obj = FreqStack()27# obj.push(val)28# param_2 = obj.pop()