Skip to content

Commit 9828736

Browse files
committed
895
1 parent b4f38ad commit 9828736

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
361361
414 | Third Maximum Number | | [Solution](leetcode/414.%20Third%20Maximum%20Number)
362362
415 | Add Strings | Easy | [Solution](leetcode/415.%20Add%20Strings)
363363
416 | Partition Equal Subset Sum | Medium | [Solution](leetcode/416.%20Partition%20Equal%20Subset%20Sum)
364-
417 | Pacific Atlantic Water Flow | | [Solution](leetcode/417.%20Pacific%20Atlantic%20Water%20Flow)
364+
417 | Pacific Atlantic Water Flow | Medium | [Solution](leetcode/417.%20Pacific%20Atlantic%20Water%20Flow)
365365
419 | Battleships in a Board | | [Solution](leetcode/419.%20Battleships%20in%20a%20Board)
366366
420 | Strong Password Checker | | [Solution](leetcode/420.%20Strong%20Password%20Checker)
367367
421 | Maximum XOR of Two Numbers in an Array | Medium | [Solution](leetcode/421.%20Maximum%20XOR%20of%20Two%20Numbers%20in%20an%20Array)
@@ -715,6 +715,7 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
715715
892 | Surface Area of 3D Shapes | Easy | [Solution](leetcode/892.%20Surface%20Area%20of%203D%20Shapes)
716716
893 | Groups of Special-Equivalent Strings | Easy | [Solution](leetcode/893.%20Groups%20of%20Special-Equivalent%20Strings)
717717
894 | All Possible Full Binary Trees | Medium | [Solution](leetcode/894.%20All%20Possible%20Full%20Binary%20Trees)
718+
895 | Maximum Frequency Stack | Hard | [Solution](leetcode/895.%20Maximum%20Frequency%20Stack)
718719
896 | Monotonic Array | Easy | [Solution](leetcode/896.%20Monotonic%20Array)
719720
897 | Increasing Order Search Tree | Easy | [Solution](leetcode/897.%20Increasing%20Order%20Search%20Tree)
720721
899 | Orderly Queue | Hard | [Solution](leetcode/899.%20Orderly%20Queue)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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>&nbsp;</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>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>0 &lt;= val &lt;= 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

Comments
 (0)