|
1 | 1 | [#0895-maximum-frequency-stack] |
2 | | -= 895. Maximum Frequency Stack |
| 2 | += 895. 最大频率栈 |
3 | 3 |
|
4 | | -{leetcode}/problems/maximum-frequency-stack/[LeetCode - Maximum Frequency Stack^] |
| 4 | +https://leetcode.cn/problems/maximum-frequency-stack/[LeetCode - 895. 最大频率栈^] |
5 | 5 |
|
6 | | -Implement `FreqStack`, a class which simulates the operation of a stack-like data structure. |
| 6 | +设计一个类似堆栈的数据结构,将元素推入堆栈,并从堆栈中弹出**出现频率**最高的元素。 |
7 | 7 |
|
8 | | -`FreqStack` has two functions: |
| 8 | +实现 `FreqStack` 类: |
9 | 9 |
|
| 10 | +* `FreqStack()` 构造一个空的堆栈。 |
| 11 | +* `void push(int val)` 将一个整数 `val` 压入栈顶。 |
| 12 | +* `int pop()` 删除并返回堆栈中出现频率最高的元素。 |
| 13 | +** 如果出现频率最高的元素不只一个,则移除并返回最接近栈顶的元素。 |
10 | 14 |
|
11 | | -* `push(int x)`, which pushes an integer `x` onto the stack. |
12 | | -* `pop()`, which *removes* and returns the most frequent element in the stack. |
13 | | - |
14 | | - * If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned. |
15 | | - |
16 | | - |
| 15 | +*示例 1:* |
17 | 16 |
|
| 17 | +.... |
| 18 | +输入: |
| 19 | +["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"], |
| 20 | +[[],[5],[7],[5],[7],[4],[5],[],[],[],[]] |
| 21 | +输出:[null,null,null,null,null,null,null,5,7,5,4] |
| 22 | +解释: |
| 23 | +FreqStack = new FreqStack(); |
| 24 | +freqStack.push (5);//堆栈为 [5] |
| 25 | +freqStack.push (7);//堆栈是 [5,7] |
| 26 | +freqStack.push (5);//堆栈是 [5,7,5] |
| 27 | +freqStack.push (7);//堆栈是 [5,7,5,7] |
| 28 | +freqStack.push (4);//堆栈是 [5,7,5,7,4] |
| 29 | +freqStack.push (5);//堆栈是 [5,7,5,7,4,5] |
| 30 | +freqStack.pop ();//返回 5 ,因为 5 出现频率最高。堆栈变成 [5,7,5,7,4]。 |
| 31 | +freqStack.pop ();//返回 7 ,因为 5 和 7 出现频率最高,但7最接近顶部。堆栈变成 [5,7,5,4]。 |
| 32 | +freqStack.pop ();//返回 5 ,因为 5 出现频率最高。堆栈变成 [5,7,4]。 |
| 33 | +freqStack.pop ();//返回 4 ,因为 4, 5 和 7 出现频率最高,但 4 是最接近顶部的。堆栈变成 [5,7]。 |
| 34 | +.... |
18 | 35 |
|
19 | | - |
| 36 | +*提示:* |
20 | 37 |
|
21 | | -*Example 1:* |
| 38 | +* `0 \<= val \<= 10^9^` |
| 39 | +* `push` 和 `pop` 的操作数不大于 `2 * 10^4^`。 |
| 40 | +* 输入保证在调用 `pop` 之前堆栈中至少有一个元素。 |
22 | 41 |
|
23 | | -[subs="verbatim,quotes,macros"] |
24 | | ----- |
25 | | -*Input:* |
26 | | - ["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"], |
27 | | - [[],[5],[7],[5],[7],[4],[5],[],[],[],[]] |
28 | | -*Output:* [null,null,null,null,null,null,null,5,7,5,4] |
29 | | -*Explanation:* |
30 | | -After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top. Then: |
31 | | -
|
32 | | -pop() -> returns 5, as 5 is the most frequent. |
33 | | -The stack becomes [5,7,5,7,4]. |
34 | 42 |
|
35 | | -pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top. |
36 | | -The stack becomes [5,7,5,4]. |
| 43 | +== 思路分析 |
37 | 44 |
|
38 | | -pop() -> returns 5. |
39 | | -The stack becomes [5,7,4]. |
| 45 | +只想到使用哈希记录出现次数,没想到可以根据出现次数去建立多个栈。 |
40 | 46 |
|
41 | | -pop() -> returns 4. |
42 | | -The stack becomes [5,7]. |
43 | | ----- |
| 47 | +image::images/0895-10.png[{image_attr}] |
44 | 48 |
|
45 | | - |
| 49 | +image::images/0895-11.png[{image_attr}] |
46 | 50 |
|
47 | | -*Note:* |
| 51 | +image::images/0895-12.png[{image_attr}] |
48 | 52 |
|
| 53 | +image::images/0895-13.png[{image_attr}] |
49 | 54 |
|
50 | | -* Calls to `FreqStack.push(int x)` will be such that `0 <= x <= 10^9`. |
51 | | -* It is guaranteed that `FreqStack.pop()` won't be called if the stack has zero elements. |
52 | | -* The total number of `FreqStack.push` calls will not exceed `10000` in a single test case. |
53 | | -* The total number of `FreqStack.pop` calls will not exceed `10000` in a single test case. |
54 | | -* The total number of `FreqStack.push` and `FreqStack.pop` calls will not exceed `150000` across all test cases. |
| 55 | +image::images/0895-14.png[{image_attr}] |
55 | 56 |
|
| 57 | +image::images/0895-15.png[{image_attr}] |
56 | 58 |
|
| 59 | +image::images/0895-16.png[{image_attr}] |
57 | 60 |
|
58 | | - |
| 61 | +image::images/0895-17.png[{image_attr}] |
59 | 62 |
|
| 63 | +image::images/0895-18.png[{image_attr}] |
60 | 64 |
|
| 65 | +image::images/0895-19.png[{image_attr}] |
61 | 66 |
|
| 67 | +image::images/0895-10.png[{image_attr}] |
62 | 68 |
|
63 | 69 | [[src-0895]] |
| 70 | +[tabs] |
| 71 | +==== |
| 72 | +一刷:: |
| 73 | ++ |
| 74 | +-- |
64 | 75 | [{java_src_attr}] |
65 | 76 | ---- |
66 | 77 | include::{sourcedir}/_0895_MaximumFrequencyStack.java[tag=answer] |
67 | 78 | ---- |
| 79 | +-- |
| 80 | +
|
| 81 | +// 二刷:: |
| 82 | +// + |
| 83 | +// -- |
| 84 | +// [{java_src_attr}] |
| 85 | +// ---- |
| 86 | +// include::{sourcedir}/_0895_MaximumFrequencyStack_2.java[tag=answer] |
| 87 | +// ---- |
| 88 | +// -- |
| 89 | +==== |
| 90 | + |
| 91 | + |
| 92 | +== 参考资料 |
| 93 | + |
68 | 94 |
|
| 95 | +. https://leetcode.cn/problems/maximum-frequency-stack/solutions/1998430/mei-xiang-ming-bai-yi-ge-dong-hua-miao-d-oich/[895. 最大频率栈 - 【动画】没想明白?一个动画秒懂!^] |
| 96 | +. https://leetcode.cn/problems/maximum-frequency-stack/solutions/1997251/zui-da-pin-lu-zhan-by-leetcode-solution-moay/[895. 最大频率栈 - 官方题解^] |
| 97 | +. https://leetcode.cn/problems/maximum-frequency-stack/solutions/1998468/-by-muse-77-bzg6/[895. 最大频率栈 - 图解LeetCode^] |
0 commit comments