LeetCode 솔루션 분류
[5/23] 703. Kth Largest Element in a Stream
본문
관련자료
-
링크
댓글 2
JayShin님의 댓글
- 익명
- 작성일
# Time Complexity: O(log n), Space Complexity: O(k)
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.k = k
self.heap = []
for num in nums:
self.add(num)
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]