We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9d5e1bd commit 444c9d0Copy full SHA for 444c9d0
kotlin/0895-maximum-frequency-stack.kt
@@ -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