You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
하드 치고는 꽤 쉬운 문제.
문제 그대로 많이 나온 원소부터 pop이 되도록 구현하면 되는데 원소가 나온 카운트에 대한 맵을 하나 두고 스택도 카운트 별로 만들어 둔다. 예제에서 push, pop 쿼리가 최대 2만개 까지라고 했으므로 20001개의 스택을 만들어 두었다.
push의 경우 원소의 개수에 대한 스택에 집어넣으면 되고 최대 카운트를 갱신해준다. pop 할 때는 최대 카운트의 스택에서 하나씩 빼주고 없다면 최대 카운트를 1 줄여주면 된다.
Source Code
importcollectionsclassFreqStack:
def__init__(self):
self.count=collections.defaultdict(int)
self.stack= [collections.deque() for_inrange(20000)]
self.max_freq=0defpush(self, val: int) ->None:
count=self.count[val] ifvalinself.countelse0self.count[val] =count+1ifcount+1>self.max_freq:
self.max_freq=count+1self.stack[count+1].append(val)
defpop(self) ->int:
val=self.stack[self.max_freq].pop()
self.count[val] -=1ifnotself.stack[self.max_freq]:
self.max_freq-=1returnval# Your FreqStack object will be instantiated and called as such:# obj = FreqStack()# obj.push(val)# param_2 = obj.pop()
This discussion was converted from issue #45 on September 15, 2026 11:02.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Problem link
https://leetcode.com/problems/maximum-frequency-stack/
Problem Summary
많이 나온 원소부터 pop이 되는 스택을 구현하는 것이다.
Solution
하드 치고는 꽤 쉬운 문제.
문제 그대로 많이 나온 원소부터 pop이 되도록 구현하면 되는데 원소가 나온 카운트에 대한 맵을 하나 두고 스택도 카운트 별로 만들어 둔다. 예제에서 push, pop 쿼리가 최대 2만개 까지라고 했으므로 20001개의 스택을 만들어 두었다.
push의 경우 원소의 개수에 대한 스택에 집어넣으면 되고 최대 카운트를 갱신해준다. pop 할 때는 최대 카운트의 스택에서 하나씩 빼주고 없다면 최대 카운트를 1 줄여주면 된다.
Source Code
All reactions