Skip to content

Commit 7ff8504

Browse files
chlng28
1 parent aabce18 commit 7ff8504

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

Challenges/2021/February-LeetCoding-Challenge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ None
5252
|581.|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Python](/Medium/581.ShortestUnsortedContinuousSubarray.py)|Medium|
5353
|946.|[Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/)|[Python](/Medium/946.ValidateStackSequences.py)|Medium|
5454
|29.|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[Python](/Medium/29.DivideTwoIntegers.py)|Medium|
55+
|895.|[Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/)|[Python](/Hard/895.MaximumFrequencyStack.py)|Hard|
5556

5657
## License
5758
The code is open-source and licensed under the [MIT License](/LICENSE).

Hard/895.MaximumFrequencyStack.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'''
2+
Implement FreqStack, a class which simulates the operation
3+
of a stack-like data structure.
4+
5+
FreqStack has two functions:
6+
7+
- push(int x), which pushes an integer x onto the
8+
stack.
9+
- pop(), which removes and returns the most frequent
10+
element in the stack.
11+
= If there is a tie for most frequent element,
12+
the element closest to the top of the stack is
13+
removed and returned.
14+
15+
Example:
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
22+
is [5,7,5,7,4,5] from bottom to top. Then:
23+
24+
pop() -> returns 5, as 5 is the most frequent.
25+
The stack becomes [5,7,5,7,4].
26+
27+
pop() -> returns 7, as 5 and 7 is the most
28+
frequent, but 7 is closest to the top.
29+
The stack becomes [5,7,5,4].
30+
31+
pop() -> returns 5.
32+
The stack becomes [5,7,4].
33+
34+
pop() -> returns 4.
35+
The stack becomes [5,7].
36+
37+
Note:
38+
- Calls to FreqStack.push(int x) will be such that
39+
0 <= x <= 10^9.
40+
- It is guaranteed that FreqStack.pop() won't be
41+
called if the stack has zero elements.
42+
- The total number of FreqStack.push calls will not
43+
exceed 10000 in a single test case.
44+
- The total number of FreqStack.pop calls will not
45+
exceed 10000 in a single test case.
46+
- The total number of FreqStack.push and FreqStack.pop
47+
calls will not exceed 150000 across all test cases.
48+
'''
49+
#Dfficulty: Hard
50+
#37 / 37 test cases passed.
51+
#Runtime: 9076 ms
52+
#Memory Usage: 22.3 MB
53+
54+
#Runtime: 9076 ms, faster than 5.05% of Python3 online submissions for Maximum Frequency Stack.
55+
#Memory Usage: 22.3 MB, less than 64.04% of Python3 online submissions for Maximum Frequency Stack.
56+
57+
class FreqStack:
58+
59+
def __init__(self):
60+
self.stack = []
61+
self.count = {}
62+
63+
def push(self, x: int) -> None:
64+
if x not in self.count:
65+
self.count[x] = 1
66+
else:
67+
self.count[x] += 1
68+
self.stack.append((x, self.count[x]))
69+
70+
def pop(self) -> int:
71+
max_count = max(self.count, key = self.count.get)
72+
length = len(self.stack)
73+
while length:
74+
length -= 1
75+
if self.stack[length][1] == self.count[max_count]:
76+
self.count[max_count] -= 1
77+
return self.stack.pop(length)[0]
78+
79+
80+
# Your FreqStack object will be instantiated and called as such:
81+
# obj = FreqStack()
82+
# obj.push(x)
83+
# param_2 = obj.pop()

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-513%2F1609-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-514%2F1609-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -20,7 +20,7 @@ In this repository provided my Python solutions of LeetCode problems.
2020

2121
2021:
2222
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
23-
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 22/28
23+
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
2424
<br><br>
2525
## Solutions
2626
*P.S. If you like this, please leave me a star.*
@@ -328,6 +328,7 @@ In this repository provided my Python solutions of LeetCode problems.
328328
|881.|[Boats to Save People](https://leetcode.com/problems/boats-to-save-people/)|[Python](/Medium/881.BoatstoSavePeople.py)|Medium|`Two Pointers`|
329329
|884.|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Python](/Easy/884.UncommonWordsfromTwoSentences.py)|Easy|simple counter|
330330
|890.|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Python](/Medium/890.FindandReplacePattern.py)|Medium|`Dictionary`|
331+
|895.|[Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/)|[Python](/Hard/895.MaximumFrequencyStack.py)|Hard||
331332
|896.|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Python](/Easy/896.MonotonicArray.py)|Easy||
332333
|897.|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Python](/Easy/897.IncreasingOrderSearchTree.py)|Easy|`DFS`, iteratively|
333334
|900.|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Python](/Medium/900.RLEIterator.py)|Medium||

0 commit comments

Comments
 (0)