Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 편스토랑 우승상품
- github
- 프로그래머스 파이썬
- Docker
- 편스토랑
- 파이썬
- 백준
- AI 경진대회
- ubuntu
- PYTHON
- 우분투
- 더현대서울 맛집
- programmers
- 자연어처리
- SW Expert Academy
- hackerrank
- gs25
- Real or Not? NLP with Disaster Tweets
- dacon
- 프로그래머스
- leetcode
- Git
- ChatGPT
- 데이콘
- 맥북
- 코로나19
- 금융문자분석경진대회
- Kaggle
- Baekjoon
- 캐치카페
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 703. Kth Largest Element in a Stream (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 703. Kth Largest Element in a Stream (Python)
솜씨좋은장씨 2020. 9. 21. 00:44728x90
반응형
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.
Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
Example:
int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3); // returns 4
kthLargest.add(5); // returns 5
kthLargest.add(10); // returns 5
kthLargest.add(9); // returns 8
kthLargest.add(4); // returns 8
Note:
You may assume that nums' length ≥ k-1 and k ≥ 1.
Solution
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.nums = nums
self.k = k
def add(self, val: int) -> int:
self.nums.append(val)
self.nums.sort()
return self.nums[len(self.nums)-self.k]
# Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1207. Unique Number of Occurrences (Python) (0) | 2020.09.23 |
---|---|
[leetCode] 657. Robot Return to Origin (Python) (0) | 2020.09.22 |
[leetCode] 1189. Maximum Number of Balloons (Python) (0) | 2020.09.20 |
[leetCode] 645. Set Mismatch (Python) (0) | 2020.09.19 |
[leetCode] 191. Number of 1 Bits (Python) (0) | 2020.09.18 |
Comments