Skip to content

Commit 06debff

Browse files
committed
一刷895
1 parent 00f711a commit 06debff

17 files changed

Lines changed: 128 additions & 47 deletions

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6289,14 +6289,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
62896289
//|{doc_base_url}/0894-all-possible-full-binary-trees.adoc[题解]
62906290
//|Medium
62916291
//|
6292-
//
6293-
//|{counter:codes}
6294-
//|{leetcode_base_url}/maximum-frequency-stack/[895. Maximum Frequency Stack^]
6295-
//|{source_base_url}/_0895_MaximumFrequencyStack.java[Java]
6296-
//|{doc_base_url}/0895-maximum-frequency-stack.adoc[题解]
6297-
//|Hard
6298-
//|
6299-
//
6292+
6293+
|{counter:codes}
6294+
|{leetcode_base_url}/maximum-frequency-stack/[895. Maximum Frequency Stack^]
6295+
|{source_base_url}/_0895_MaximumFrequencyStack.java[Java]
6296+
|{doc_base_url}/0895-maximum-frequency-stack.adoc[题解]
6297+
|Hard
6298+
|
6299+
63006300
//|{counter:codes}
63016301
//|{leetcode_base_url}/monotonic-array/[896. Monotonic Array^]
63026302
//|{source_base_url}/_0896_MonotonicArray.java[Java]
Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,97 @@
11
[#0895-maximum-frequency-stack]
2-
= 895. Maximum Frequency Stack
2+
= 895. 最大频率栈
33

4-
{leetcode}/problems/maximum-frequency-stack/[LeetCode - Maximum Frequency Stack^]
4+
https://leetcode.cn/problems/maximum-frequency-stack/[LeetCode - 895. 最大频率栈^]
55

6-
Implement `FreqStack`, a class which simulates the operation of a stack-like data structure.
6+
设计一个类似堆栈的数据结构,将元素推入堆栈,并从堆栈中弹出**出现频率**最高的元素。
77

8-
`FreqStack` has two functions:
8+
实现 `FreqStack` :
99

10+
* `FreqStack()` 构造一个空的堆栈。
11+
* `void push(int val)` 将一个整数 `val` 压入栈顶。
12+
* `int pop()` 删除并返回堆栈中出现频率最高的元素。
13+
** 如果出现频率最高的元素不只一个,则移除并返回最接近栈顶的元素。
1014
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:*
1716

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+
....
1835

19-
36+
*提示:*
2037

21-
*Example 1:*
38+
* `0 \<= val \<= 10^9^`
39+
* `push``pop` 的操作数不大于 `2 * 10^4^`
40+
* 输入保证在调用 `pop` 之前堆栈中至少有一个元素。
2241
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].
3442
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+
== 思路分析
3744

38-
pop() -> returns 5.
39-
The stack becomes [5,7,4].
45+
只想到使用哈希记录出现次数,没想到可以根据出现次数去建立多个栈。
4046

41-
pop() -> returns 4.
42-
The stack becomes [5,7].
43-
----
47+
image::images/0895-10.png[{image_attr}]
4448

45-
49+
image::images/0895-11.png[{image_attr}]
4650

47-
*Note:*
51+
image::images/0895-12.png[{image_attr}]
4852

53+
image::images/0895-13.png[{image_attr}]
4954

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}]
5556

57+
image::images/0895-15.png[{image_attr}]
5658

59+
image::images/0895-16.png[{image_attr}]
5760

58-
61+
image::images/0895-17.png[{image_attr}]
5962

63+
image::images/0895-18.png[{image_attr}]
6064

65+
image::images/0895-19.png[{image_attr}]
6166

67+
image::images/0895-10.png[{image_attr}]
6268

6369
[[src-0895]]
70+
[tabs]
71+
====
72+
一刷::
73+
+
74+
--
6475
[{java_src_attr}]
6576
----
6677
include::{sourcedir}/_0895_MaximumFrequencyStack.java[tag=answer]
6778
----
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+
6894

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^]

docs/images/0895-10.png

76.4 KB
Loading

docs/images/0895-11.png

56.6 KB
Loading

docs/images/0895-12.png

58.8 KB
Loading

docs/images/0895-13.png

60.4 KB
Loading

docs/images/0895-14.png

61 KB
Loading

docs/images/0895-15.png

62 KB
Loading

docs/images/0895-16.png

63.7 KB
Loading

docs/images/0895-17.png

80.7 KB
Loading

0 commit comments

Comments
 (0)