Skip to content

Commit e8592e8

Browse files
committed
703
1 parent 94e2c64 commit e8592e8

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'rb_heap'
2+
3+
class KthLargest
4+
def initialize(k, nums)
5+
@k = k
6+
@heap = Heap.new
7+
nums.each { |num| @heap << num }
8+
@heap.pop until @heap.size <= @k
9+
end
10+
11+
def add(val)
12+
@heap << val
13+
@heap.pop until @heap.size <= @k
14+
@heap.peak
15+
end
16+
end

0 commit comments

Comments
 (0)