1. ๋ฌธ์ : https://leetcode.com/problems/kth-largest-element-in-a-stream/description/
2. ํ์ด
pq์ ๊ธธ์ด ์ ํ์ผ๋ก ๊ฐ์ ์ปจํธ๋กคํ๋ ๋ฐฉ์์ ์๊ฐํด์ผํ๋ ๋ฌธ์
3. ์ฝ๋
import java.util.*;
public class KthLargest {
private final PriorityQueue<Integer> q;
private final int k;
public KthLargest(int k, int[] nums) {
this.k = k;
q = new PriorityQueue<Integer>();
for (int a :nums) {
add(a);
}
}
public int add(int val) {
q.offer(val);
if (q.size() > k) {
q.poll();
}
return q.peek();
}
}
'๐ป Coding Problems Solving > Stack | Queue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ํธํ ๋์ค (์๋ฐ java) (0) | 2023.12.02 |
---|---|
[LeetCode] FindKPairsWithSmallestSums (0) | 2023.07.17 |
[LeetCode] Add Two Numbers (0) | 2023.07.14 |
[BOJ 1863] ์ค์นด์ด๋ผ์ธ ์ฌ์ด๊ฑฐ (0) | 2023.06.07 |
[BOJ 11000] ๊ฐ์์ค ๋ฐฐ์ (0) | 2023.05.25 |
์ต๊ทผ๋๊ธ