일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- 사이드
- 개발자
- 월간
- 10분
- FIT XR
- Writing
- 스탭퍼
- 매일
- 링피트
- 영어원서읽기
- realclass
- 운동
- 3줄정리
- 리얼 클래스
- 화상영어
- Problem Solving
- 잡생각
- 프로젝트
- 읽기
- leetcode
- 30분
- 영어공부
- 미드시청
- 쓰릴오브파이트
- 파비최
- 괜찮음
- Daily Challenge
- 만화도
- English
- 뭐든
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