Skip to content

Commit 3e2d313

Browse files
committed
895 added
1 parent 8417d06 commit 3e2d313

File tree

4 files changed

+122
-27
lines changed

4 files changed

+122
-27
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [895. Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/)
2+
3+
## 题目
4+
5+
Implement FreqStack, a class which simulates the operation of a stack-like data structure.
6+
7+
FreqStack`has two functions:
8+
9+
- push(int x), which pushes an integer x onto the stack.
10+
- pop(), which removes and returns the most frequent element in the stack.
11+
- If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.
12+
13+
Example 1:
14+
15+
```text
16+
Input:
17+
["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"],
18+
[[],[5],[7],[5],[7],[4],[5],[],[],[],[]]
19+
Output: [null,null,null,null,null,null,null,5,7,5,4]
20+
Explanation:
21+
After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top. Then:
22+
23+
pop() -> returns 5, as 5 is the most frequent.
24+
The stack becomes [5,7,5,7,4].
25+
26+
pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top.
27+
The stack becomes [5,7,5,4].
28+
29+
pop() -> returns 5.
30+
The stack becomes [5,7,4].
31+
32+
pop() -> returns 4.
33+
The stack becomes [5,7].
34+
```
35+
36+
Note:
37+
38+
- Calls to FreqStack.push(int x)`will be such that 0 <= x <= 10^9.
39+
- It is guaranteed that FreqStack.pop() won't be called if the stack has zero elements.
40+
- The total number of FreqStack.push calls will not exceed 10000 in a single test case.
41+
- The total number of FreqStack.pop`calls will not exceed 10000 in a single test case.
42+
- The total number of FreqStack.push and FreqStack.pop calls will not exceed 150000 across all test cases.
43+
44+
## 解题思路
45+
46+
见程序注释
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package problem0895
2+
3+
// FreqStack object will be instantiated and called as such:
4+
// obj := Constructor();
5+
// obj.Push(x);
6+
// param_2 := obj.Pop();
7+
type FreqStack struct {
8+
}
9+
10+
// Constructor 构建 FreqStack
11+
func Constructor() *FreqStack {
12+
13+
return &FreqStack{}
14+
}
15+
16+
// Push 在 fs 中放入 x
17+
func (fs *FreqStack) Push(x int) {
18+
19+
}
20+
21+
// Pop 从 fs 中弹出元素
22+
func (fs *FreqStack) Pop() int {
23+
return 0
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package problem0895
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_FreqStack(t *testing.T) {
10+
ast := assert.New(t)
11+
12+
fs := Constructor()
13+
14+
pushes := []int{5, 7, 5, 7, 4, 5}
15+
for _, p := range pushes {
16+
fs.Push(p)
17+
}
18+
19+
expecteds := []int{5, 7, 4, 5}
20+
for _, expected := range expecteds {
21+
actual := fs.Pop()
22+
ast.Equal(expected, actual)
23+
}
24+
25+
}

leetcode.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 819,
4-
"Updated": "2018-10-07T18:59:57.366349674+08:00",
3+
"Ranking": 814,
4+
"Updated": "2018-10-08T19:45:46.222634889+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 199,
@@ -2509,7 +2509,7 @@
25092509
"ID": 207,
25102510
"Title": "Course Schedule",
25112511
"TitleSlug": "course-schedule",
2512-
"PassRate": "34%",
2512+
"PassRate": "35%",
25132513
"Difficulty": "Medium",
25142514
"IsAccepted": true,
25152515
"IsPaid": false,
@@ -2569,7 +2569,7 @@
25692569
"ID": 212,
25702570
"Title": "Word Search II",
25712571
"TitleSlug": "word-search-ii",
2572-
"PassRate": "25%",
2572+
"PassRate": "26%",
25732573
"Difficulty": "Hard",
25742574
"IsAccepted": true,
25752575
"IsPaid": false,
@@ -2725,7 +2725,7 @@
27252725
"ID": 225,
27262726
"Title": "Implement Stack using Queues",
27272727
"TitleSlug": "implement-stack-using-queues",
2728-
"PassRate": "35%",
2728+
"PassRate": "36%",
27292729
"Difficulty": "Easy",
27302730
"IsAccepted": true,
27312731
"IsPaid": false,
@@ -2929,7 +2929,7 @@
29292929
"ID": 242,
29302930
"Title": "Valid Anagram",
29312931
"TitleSlug": "valid-anagram",
2932-
"PassRate": "48%",
2932+
"PassRate": "49%",
29332933
"Difficulty": "Easy",
29342934
"IsAccepted": true,
29352935
"IsPaid": false,
@@ -4969,7 +4969,7 @@
49694969
"ID": 412,
49704970
"Title": "Fizz Buzz",
49714971
"TitleSlug": "fizz-buzz",
4972-
"PassRate": "57%",
4972+
"PassRate": "58%",
49734973
"Difficulty": "Easy",
49744974
"IsAccepted": true,
49754975
"IsPaid": false,
@@ -5137,7 +5137,7 @@
51375137
"ID": 426,
51385138
"Title": "Convert Binary Search Tree to Sorted Doubly Linked List",
51395139
"TitleSlug": "convert-binary-search-tree-to-sorted-doubly-linked-list",
5140-
"PassRate": "38%",
5140+
"PassRate": "39%",
51415141
"Difficulty": "Medium",
51425142
"IsAccepted": false,
51435143
"IsPaid": true,
@@ -5437,7 +5437,7 @@
54375437
"ID": 451,
54385438
"Title": "Sort Characters By Frequency",
54395439
"TitleSlug": "sort-characters-by-frequency",
5440-
"PassRate": "52%",
5440+
"PassRate": "53%",
54415441
"Difficulty": "Medium",
54425442
"IsAccepted": true,
54435443
"IsPaid": false,
@@ -6349,7 +6349,7 @@
63496349
"ID": 527,
63506350
"Title": "Word Abbreviation",
63516351
"TitleSlug": "word-abbreviation",
6352-
"PassRate": "44%",
6352+
"PassRate": "45%",
63536353
"Difficulty": "Hard",
63546354
"IsAccepted": false,
63556355
"IsPaid": true,
@@ -7333,7 +7333,7 @@
73337333
"ID": 609,
73347334
"Title": "Find Duplicate File in System",
73357335
"TitleSlug": "find-duplicate-file-in-system",
7336-
"PassRate": "52%",
7336+
"PassRate": "53%",
73377337
"Difficulty": "Medium",
73387338
"IsAccepted": true,
73397339
"IsPaid": false,
@@ -7741,7 +7741,7 @@
77417741
"ID": 643,
77427742
"Title": "Maximum Average Subarray I",
77437743
"TitleSlug": "maximum-average-subarray-i",
7744-
"PassRate": "38%",
7744+
"PassRate": "37%",
77457745
"Difficulty": "Easy",
77467746
"IsAccepted": true,
77477747
"IsPaid": false,
@@ -8497,7 +8497,7 @@
84978497
"ID": 706,
84988498
"Title": "Design HashMap",
84998499
"TitleSlug": "design-hashmap",
8500-
"PassRate": "45%",
8500+
"PassRate": "46%",
85018501
"Difficulty": "Easy",
85028502
"IsAccepted": true,
85038503
"IsPaid": false,
@@ -8773,7 +8773,7 @@
87738773
"ID": 729,
87748774
"Title": "My Calendar I",
87758775
"TitleSlug": "my-calendar-i",
8776-
"PassRate": "43%",
8776+
"PassRate": "44%",
87778777
"Difficulty": "Medium",
87788778
"IsAccepted": true,
87798779
"IsPaid": false,
@@ -9037,7 +9037,7 @@
90379037
"ID": 751,
90389038
"Title": "IP to CIDR",
90399039
"TitleSlug": "ip-to-cidr",
9040-
"PassRate": "52%",
9040+
"PassRate": "53%",
90419041
"Difficulty": "Easy",
90429042
"IsAccepted": false,
90439043
"IsPaid": true,
@@ -9109,7 +9109,7 @@
91099109
"ID": 757,
91109110
"Title": "Set Intersection Size At Least Two",
91119111
"TitleSlug": "set-intersection-size-at-least-two",
9112-
"PassRate": "34%",
9112+
"PassRate": "35%",
91139113
"Difficulty": "Hard",
91149114
"IsAccepted": true,
91159115
"IsPaid": false,
@@ -9265,7 +9265,7 @@
92659265
"ID": 770,
92669266
"Title": "Basic Calculator IV",
92679267
"TitleSlug": "basic-calculator-iv",
9268-
"PassRate": "41%",
9268+
"PassRate": "42%",
92699269
"Difficulty": "Hard",
92709270
"IsAccepted": true,
92719271
"IsPaid": false,
@@ -9865,7 +9865,7 @@
98659865
"ID": 820,
98669866
"Title": "Short Encoding of Words",
98679867
"TitleSlug": "short-encoding-of-words",
9868-
"PassRate": "43%",
9868+
"PassRate": "44%",
98699869
"Difficulty": "Medium",
98709870
"IsAccepted": true,
98719871
"IsPaid": false,
@@ -10009,7 +10009,7 @@
1000910009
"ID": 832,
1001010010
"Title": "Flipping an Image",
1001110011
"TitleSlug": "flipping-an-image",
10012-
"PassRate": "68%",
10012+
"PassRate": "69%",
1001310013
"Difficulty": "Easy",
1001410014
"IsAccepted": true,
1001510015
"IsPaid": false,
@@ -10369,7 +10369,7 @@
1036910369
"ID": 862,
1037010370
"Title": "Shortest Subarray with Sum at Least K",
1037110371
"TitleSlug": "shortest-subarray-with-sum-at-least-k",
10372-
"PassRate": "18%",
10372+
"PassRate": "19%",
1037310373
"Difficulty": "Hard",
1037410374
"IsAccepted": true,
1037510375
"IsPaid": false,
@@ -10393,7 +10393,7 @@
1039310393
"ID": 864,
1039410394
"Title": "Shortest Path to Get All Keys",
1039510395
"TitleSlug": "shortest-path-to-get-all-keys",
10396-
"PassRate": "33%",
10396+
"PassRate": "32%",
1039710397
"Difficulty": "Hard",
1039810398
"IsAccepted": true,
1039910399
"IsPaid": false,
@@ -10405,7 +10405,7 @@
1040510405
"ID": 865,
1040610406
"Title": "Smallest Subtree with all the Deepest Nodes",
1040710407
"TitleSlug": "smallest-subtree-with-all-the-deepest-nodes",
10408-
"PassRate": "51%",
10408+
"PassRate": "52%",
1040910409
"Difficulty": "Medium",
1041010410
"IsAccepted": true,
1041110411
"IsPaid": false,
@@ -10525,7 +10525,7 @@
1052510525
"ID": 875,
1052610526
"Title": "Koko Eating Bananas",
1052710527
"TitleSlug": "koko-eating-bananas",
10528-
"PassRate": "41%",
10528+
"PassRate": "42%",
1052910529
"Difficulty": "Medium",
1053010530
"IsAccepted": true,
1053110531
"IsPaid": false,
@@ -10633,7 +10633,7 @@
1063310633
"ID": 884,
1063410634
"Title": "Uncommon Words from Two Sentences",
1063510635
"TitleSlug": "uncommon-words-from-two-sentences",
10636-
"PassRate": "60%",
10636+
"PassRate": "59%",
1063710637
"Difficulty": "Easy",
1063810638
"IsAccepted": true,
1063910639
"IsPaid": false,
@@ -10741,7 +10741,7 @@
1074110741
"ID": 893,
1074210742
"Title": "Groups of Special-Equivalent Strings",
1074310743
"TitleSlug": "groups-of-special-equivalent-strings",
10744-
"PassRate": "60%",
10744+
"PassRate": "61%",
1074510745
"Difficulty": "Easy",
1074610746
"IsAccepted": true,
1074710747
"IsPaid": false,
@@ -11041,7 +11041,7 @@
1104111041
"ID": 918,
1104211042
"Title": "Maximum Sum Circular Subarray",
1104311043
"TitleSlug": "maximum-sum-circular-subarray",
11044-
"PassRate": "20%",
11044+
"PassRate": "23%",
1104511045
"Difficulty": "Medium",
1104611046
"IsAccepted": false,
1104711047
"IsPaid": false,
@@ -11065,7 +11065,7 @@
1106511065
"ID": 920,
1106611066
"Title": "Number of Music Playlists",
1106711067
"TitleSlug": "number-of-music-playlists",
11068-
"PassRate": "34%",
11068+
"PassRate": "37%",
1106911069
"Difficulty": "Hard",
1107011070
"IsAccepted": false,
1107111071
"IsPaid": false,

0 commit comments

Comments
 (0)