|
| 1 | +# 895. Maximum Frequency Stack |
| 2 | + |
| 3 | +## Intuition |
| 4 | + |
| 5 | +The problem requires us to implement a stack-like data structure that can track the frequency of elements and pop the most frequent element. When multiple elements have the same frequency, we need to pop the one that was pushed most recently. The key insight is to maintain a frequency map and a stack of stacks, where each inner stack contains elements of the same frequency. |
| 6 | + |
| 7 | +## Approach |
| 8 | + |
| 9 | +1. Use a `freqMap` to track the frequency of each element |
| 10 | +2. Use a `freqStack` which is a 2D slice where: |
| 11 | + - Each inner slice represents elements of the same frequency |
| 12 | + - The index of the inner slice represents the frequency level |
| 13 | + - Elements in the same inner slice maintain their push order |
| 14 | +3. For Push operation: |
| 15 | + - If element is new or has frequency -1, add it to frequency level 0 |
| 16 | + - Otherwise, increment its frequency and add it to the corresponding level |
| 17 | +4. For Pop operation: |
| 18 | + - Remove the last element from the highest frequency level |
| 19 | + - Decrement its frequency in the map |
| 20 | + - Remove the frequency level if it becomes empty |
| 21 | + |
| 22 | +## Complexity |
| 23 | + |
| 24 | +- Time complexity: O(1) |
| 25 | +- Space complexity: O(n) |
| 26 | + |
| 27 | +## Keywords |
| 28 | + |
| 29 | +- Stack |
| 30 | +- Hash Map |
| 31 | + |
| 32 | +## Code |
| 33 | + |
| 34 | +```go |
| 35 | +type FreqStack struct { |
| 36 | + freqMap map[int]int |
| 37 | + freqStack [][]int |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +func Constructor() FreqStack { |
| 42 | + f := FreqStack{ |
| 43 | + freqMap: make(map[int]int), |
| 44 | + freqStack: make([][]int, 0), |
| 45 | + } |
| 46 | + f.freqStack = append(f.freqStack, make([]int, 0)) |
| 47 | + return f |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +func (f *FreqStack) Push(val int) { |
| 52 | + if level, found := f.freqMap[val]; !found || level == -1 { |
| 53 | + f.freqStack[0], f.freqMap[val] = append(f.freqStack[0], val), 0 |
| 54 | + } else { |
| 55 | + if len(f.freqStack) - 1 == level { |
| 56 | + f.freqStack = append(f.freqStack, make([]int, 0)) |
| 57 | + } |
| 58 | + f.freqStack[level + 1], f.freqMap[val] = append(f.freqStack[level + 1], val), level + 1 |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +func (f *FreqStack) Pop() int { |
| 64 | + tgt := f.freqStack[len(f.freqStack) - 1][len(f.freqStack[len(f.freqStack) - 1]) - 1] |
| 65 | + f.freqStack[len(f.freqStack) - 1], f.freqMap[tgt] = f.freqStack[len(f.freqStack) - 1][: len(f.freqStack[len(f.freqStack) - 1]) - 1], f.freqMap[tgt] - 1 |
| 66 | + if len(f.freqStack[len(f.freqStack) - 1]) == 0 { |
| 67 | + f.freqStack = f.freqStack[: len(f.freqStack) - 1] |
| 68 | + } |
| 69 | + return tgt |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +/** |
| 74 | + * Your FreqStack object will be instantiated and called as such: |
| 75 | + * obj := Constructor(); |
| 76 | + * obj.Push(val); |
| 77 | + * param_2 := obj.Pop(); |
| 78 | + */ |
| 79 | +``` |
0 commit comments