Skip to content

Commit 9e10923

Browse files
committed
Create 703-Kth-Largest-Element-In-A-Stream.kt
1 parent 2e826d0 commit 9e10923

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 KthLargest(k: Int, nums: IntArray) {
2+
val minHeap = PriorityQueue<Int>{ a: Int, b: Int ->
3+
a - b
4+
}
5+
var k = Integer.MIN_VALUE
6+
init{
7+
this.k = k
8+
for(num in nums)
9+
minHeap.add(num)
10+
while(minHeap.size > k)
11+
minHeap.poll()
12+
}
13+
fun add(`val`: Int): Int {
14+
minHeap.add(`val`)
15+
if(minHeap.size > k)
16+
minHeap.poll()
17+
return minHeap.peek()
18+
}
19+
20+
}

0 commit comments

Comments
 (0)