Skip to content

Commit

Permalink
Create 0895-maximum-frequency-stack.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Jan 31, 2023
1 parent 9d5e1bd commit 444c9d0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions kotlin/0895-maximum-frequency-stack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class FreqStack() {

val countMap = HashMap<Int, Int>()
var max = 0
val stackMap = HashMap<Int, ArrayDeque<Int>>()

fun push(`val`: Int) {
val count = countMap.getOrDefault(`val`, 0) + 1
countMap[`val`] = count
if (count > max) {
max = count
stackMap.put(max, ArrayDeque())
}
stackMap[count]!!.addFirst(`val`)
}

fun pop(): Int {
val res = stackMap[max]!!.removeFirst()
countMap[res] = countMap[res]!! - 1
if(stackMap[max]!!.isEmpty()) max--
return res
}

}

/**
* Your FreqStack object will be instantiated and called as such:
* var obj = FreqStack()
* obj.push(`val`)
* var param_2 = obj.pop()
*/

0 comments on commit 444c9d0

Please sign in to comment.