FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/Python/maximum-frequency-stack.py at master · pythonpeixun/LeetCode-Solutions · GitHub
pythonpeixun
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
Python
/
maximum-frequency-stack.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
38 lines (29 loc) · 823 Bytes
Breadcrumbs
LeetCode-Solutions
/
Python
/
maximum-frequency-stack.py
Copy path
File metadata and controls
38 lines (29 loc) · 823 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Time: O(1)
# Space: O(n)
import
collections
class
FreqStack
(
object
):
def
__init__
(
self
):
self
.
__freq
=
collections
.
Counter
()
self
.
__group
=
collections
.
defaultdict
(
list
)
self
.
__maxfreq
=
0
def
push
(
self
,
x
):
"""
:type x: int
:rtype: void
"""
self
.
__freq
[
x
]
+=
1
if
self
.
__freq
[
x
]
>
self
.
__maxfreq
:
self
.
__maxfreq
=
self
.
__freq
[
x
]
self
.
__group
[
self
.
__freq
[
x
]].
append
(
x
)
def
pop
(
self
):
"""
:rtype: int
"""
x
=
self
.
__group
[
self
.
__maxfreq
].
pop
()
if
not
self
.
__group
[
self
.
__maxfreq
]:
self
.
__group
.
pop
(
self
.
__maxfreq
)
self
.
__maxfreq
-=
1
self
.
__freq
[
x
]
-=
1
if
not
self
.
__freq
[
x
]:
self
.
__freq
.
pop
(
x
)
return
x
Back
|
FazBrowse Home
|
New Git URL