Skip to content

Commit 444c9d0

Browse files
committed
Create 0895-maximum-frequency-stack.kt
1 parent 9d5e1bd commit 444c9d0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class FreqStack() {
2+
3+
val countMap = HashMap<Int, Int>()
4+
var max = 0
5+
val stackMap = HashMap<Int, ArrayDeque<Int>>()
6+
7+
fun push(`val`: Int) {
8+
val count = countMap.getOrDefault(`val`, 0) + 1
9+
countMap[`val`] = count
10+
if (count > max) {
11+
max = count
12+
stackMap.put(max, ArrayDeque())
13+
}
14+
stackMap[count]!!.addFirst(`val`)
15+
}
16+
17+
fun pop(): Int {
18+
val res = stackMap[max]!!.removeFirst()
19+
countMap[res] = countMap[res]!! - 1
20+
if(stackMap[max]!!.isEmpty()) max--
21+
return res
22+
}
23+
24+
}
25+
26+
/**
27+
* Your FreqStack object will be instantiated and called as such:
28+
* var obj = FreqStack()
29+
* obj.push(`val`)
30+
* var param_2 = obj.pop()
31+
*/

0 commit comments

Comments
 (0)