Kth Largest Element in a Stream LeetCode solution in Python

1
# Runtime: 143 ms (Top 61.21%) | Memory: 18.3 MB (Top 46.75%)
2
class KthLargest:
3
def __init__(self, k: int, nums: List[int]):
4
self.k = k
5
self.hp = []
6
for x in nums:
7
self.add(x)
8

9
return None
10

11
def add(self, val: int) -> int:
12
heapq.heappush(self.hp, (val))
13
if len(self.hp) > self.k:
14
heapq.heappop(self.hp)
15

16
return self.get_kth_largest()
17

18
def get_kth_largest(self):
19
return self.hp[0]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0