Skip to content

Commit b9d2df1

Browse files
Merge pull request #25 from theforestvn88/august
0895
2 parents 088c870 + 3bd3491 commit b9d2df1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class FreqStack
2+
def initialize()
3+
@max_freq = 0
4+
@counter = Hash.new(0)
5+
@freq = Hash.new { |h, k| h[k] = [] }
6+
end
7+
8+
def push(val)
9+
@counter[val] += 1
10+
@freq[@counter[val]] << val
11+
@max_freq = @counter[val] if @max_freq < @counter[val]
12+
end
13+
14+
def pop()
15+
val = @freq[@max_freq].pop
16+
@counter[val] -= 1
17+
@max_freq -= 1 while @max_freq > 0 && @freq[@max_freq].empty?
18+
val
19+
end
20+
end

0 commit comments

Comments
 (0)