일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 10분
- 링피트
- FIT XR
- 뭐든
- 사이드
- 미드시청
- 화상영어
- 스탭퍼
- 30분
- 괜찮음
- leetcode
- 3줄정리
- 월간
- 리얼 클래스
- 영어원서읽기
- Writing
- 프로젝트
- 잡생각
- 만화도
- 파비최
- 영어공부
- 개발자
- 운동
- Daily Challenge
- 읽기
- English
- 매일
- 쓰릴오브파이트
- realclass
- Problem Solving
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.05.23 Today's Challenge 본문
https://leetcode.com/problems/kth-largest-element-in-a-stream/
Kth Largest Element in a Stream - LeetCode
Can you solve this real interview question? Kth Largest Element in a Stream - Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. Implement KthLargest class:
leetcode.com
문제를 보는 순간 Heap을 쓰면 되는거 같았는데, 어찌 쓰면 되는지는 솔루션을 참고함
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.k = k
self.heap = nums
heapq.heapify(self.heap)
while len(self.heap) > k:
heapq.heappop(self.heap)
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]
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.05.25 Today's Challenge (0) | 2023.05.25 |
---|---|
2023.05.24 Today's Challenge (0) | 2023.05.24 |
2023.05.22 Today's Challenge (0) | 2023.05.22 |
2023.05.21 Today's Challenge (1) | 2023.05.21 |
2023.05.20 Today's Challenge (0) | 2023.05.20 |
Comments