Skip to content

Commit febbaab

Browse files
authored
Create 703-Kth-Largest-Element-in-a-Stream.py
1 parent b3efa34 commit febbaab

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class KthLargest:
2+
3+
def __init__(self, k: int, nums: List[int]):
4+
# minHeap w/ K largest integers
5+
self.minHeap, self.k = nums, k
6+
heapq.heapify(self.minHeap)
7+
while len(self.minHeap) > k:
8+
heapq.heappop(self.minHeap)
9+
10+
def add(self, val: int) -> int:
11+
heapq.heappush(self.minHeap, val)
12+
if len(self.minHeap) > self.k:
13+
heapq.heappop(self.minHeap)
14+
return self.minHeap[0]

0 commit comments

Comments
 (0)