https://leetcode.com/problems/kth-largest-element-in-a-stream/ Easy

Решение

class KthLargest(private val k: Int, nums: IntArray) {

    private val heap = java.util.PriorityQueue<Int>()

    init {
        for (x in nums) {
            if (heap.size < k) {
                heap.add(x)
            } else if (x > heap.peek()) {
                heap.poll()
                heap.add(x)
            }
        }
    }

    fun add(`val`: Int): Int {
        if (heap.size < k) {
            heap.add(`val`)
        } else if (`val` > heap.peek()) {
            heap.poll()
            heap.add(`val`)
        }
        return heap.peek()
    }
}