703. Kth Largest Element in a Stream
https://leetcode.com/problems/kth-largest-element-in-a-stream/
Python
import heapq
class KthLargest:
def __init__(self, k: int, nums: List[int]):
heap = nums[:]
heapq.heapify(heap)
for i in range(len(heap)-k):
heapq.heappop(heap)
self.heap = heap
self.k = k
def add(self, val: int) -> int:
heapq.heappush(self.heap, val)
if len(self.heap) > self.k:
heapq.heappop(self.heap)
return self.heap[0]