|
| 1 | +# [895. Maximum Frequency Stack (Hard)](https://leetcode.com/problems/maximum-frequency-stack/) |
| 2 | + |
| 3 | +<p>Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.</p> |
| 4 | + |
| 5 | +<p>Implement the <code>FreqStack</code> class:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li><code>FreqStack()</code> constructs an empty frequency stack.</li> |
| 9 | + <li><code>void push(int val)</code> pushes an integer <code>val</code> onto the top of the stack.</li> |
| 10 | + <li><code>int pop()</code> removes and returns the most frequent element in the stack. |
| 11 | + <ul> |
| 12 | + <li>If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.</li> |
| 13 | + </ul> |
| 14 | + </li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | +<p><strong>Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre><strong>Input</strong> |
| 21 | +["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"] |
| 22 | +[[], [5], [7], [5], [7], [4], [5], [], [], [], []] |
| 23 | +<strong>Output</strong> |
| 24 | +[null, null, null, null, null, null, null, 5, 7, 5, 4] |
| 25 | + |
| 26 | +<strong>Explanation</strong> |
| 27 | +FreqStack freqStack = new FreqStack(); |
| 28 | +freqStack.push(5); // The stack is [5] |
| 29 | +freqStack.push(7); // The stack is [5,7] |
| 30 | +freqStack.push(5); // The stack is [5,7,5] |
| 31 | +freqStack.push(7); // The stack is [5,7,5,7] |
| 32 | +freqStack.push(4); // The stack is [5,7,5,7,4] |
| 33 | +freqStack.push(5); // The stack is [5,7,5,7,4,5] |
| 34 | +freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4]. |
| 35 | +freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4]. |
| 36 | +freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4]. |
| 37 | +freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7]. |
| 38 | +</pre> |
| 39 | + |
| 40 | +<p> </p> |
| 41 | +<p><strong>Constraints:</strong></p> |
| 42 | + |
| 43 | +<ul> |
| 44 | + <li><code>0 <= val <= 10<sup>9</sup></code></li> |
| 45 | + <li>At most <code>2 * 10<sup>4</sup></code> calls will be made to <code>push</code> and <code>pop</code>.</li> |
| 46 | + <li>It is guaranteed that there will be at least one element in the stack before calling <code>pop</code>.</li> |
| 47 | +</ul> |
| 48 | + |
| 49 | + |
| 50 | +**Related Topics**: |
| 51 | +[Hash Table](https://leetcode.com/tag/hash-table/), [Stack](https://leetcode.com/tag/stack/) |
| 52 | + |
| 53 | +## Solution 1. |
| 54 | + |
| 55 | +We push elements based on frequency first then the order they pushed into the stack. |
| 56 | + |
| 57 | +Consider input `5 7 5 7 4 5`, if we label their frequency at the top |
| 58 | + |
| 59 | +``` |
| 60 | +1 1 2 2 1 3 |
| 61 | +5 7 5 7 4 5 |
| 62 | +``` |
| 63 | + |
| 64 | +We can reorder them in this way |
| 65 | + |
| 66 | +``` |
| 67 | +1: 5 7 4 |
| 68 | +2: 5 7 |
| 69 | +3: 5 |
| 70 | +``` |
| 71 | + |
| 72 | +When popping elements, we pop the 3rd stack first, then the 2nd, then the 1st. |
| 73 | + |
| 74 | +```cpp |
| 75 | +// OJ: https://leetcode.com/problems/maximum-frequency-stack/ |
| 76 | +// Author: github.com/lzl124631x |
| 77 | +// Time: O(1) for all |
| 78 | +// Space: O(N) |
| 79 | +class FreqStack { |
| 80 | + unordered_map<int, int> freq; |
| 81 | + vector<vector<int>> v; |
| 82 | +public: |
| 83 | + FreqStack() {} |
| 84 | + void push(int val) { |
| 85 | + int f = freq[val]++; |
| 86 | + if (v.size() <= f) v.emplace_back(); |
| 87 | + v[f].push_back(val); |
| 88 | + } |
| 89 | + int pop() { |
| 90 | + int val = v.back().back(); |
| 91 | + v.back().pop_back(); |
| 92 | + --freq[val]; |
| 93 | + if (v.back().empty()) v.pop_back(); |
| 94 | + return val; |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
0 commit comments