티스토리 뷰
Problem
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:
- KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.
- int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream.
Example 1:
Input
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output
[null, 4, 5, 5, 8, 8]
Explanation
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
Constraints:
- 1 <= k <= 10^4
- 0 <= nums.length <= 10^4
- -10^4 <= nums[i] <= 10^4
- -10^4 <= val <= 10^4
- At most 10^4 calls will be made to add.
- It is guaranteed that there will be at least k elements in the array when you search for the kth element.
Solution
스트림의 k번 째로 큰 수를 찾아주는 클래스를 구현하는 문제입니다.
먼저 생성자로 k와 초기 배열을 전달할 수 있고, 그 이후로는 원소들을 더할 수 있는데 k번째로 큰 수를 반환해야합니다.
우선순위 큐를 이용해 k개 만큼만 큐에 저장하게하고, top에 있는 엘리먼트를 반환하면 됩니다.
import java.util.PriorityQueue;
public class KthLargest {
private final PriorityQueue<Integer> queue;
private final int k;
public KthLargest(int k, int[] nums) {
queue = new PriorityQueue<>();
this.k = k;
for (int num : nums) { // (1)
queue.offer(num);
if (queue.size() > k) {
queue.poll();
}
}
}
public int add(int val) { // (2)
queue.offer(val);
if (queue.size() > k) {
queue.poll();
}
return queue.peek();
}
}
- queue에 모든 정수를 넣는데 k개를 초과하게되면 가장 작은 수를 제거합니다.
- 1번과 동일합니다.
Test
package io.lcalmsky.leetcode.kth_largest_element_in_a_stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class KthLargestTest {
@Test
void test() {
KthLargest kthLargest = new KthLargest(3, new int[]{4, 5, 8, 2});
assertEquals(4, kthLargest.add(3));
assertEquals(5, kthLargest.add(5));
assertEquals(5, kthLargest.add(10));
assertEquals(8, kthLargest.add(9));
assertEquals(8, kthLargest.add(4));
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
99. Recover Binary Search Tree (0) | 2022.05.07 |
---|---|
844. Backspace String Compare (0) | 2022.05.06 |
705. Design HashSet (0) | 2022.05.04 |
410. Split Array Largest Sum (0) | 2022.05.03 |
897. Increasing Order Search Tree (0) | 2022.04.30 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Spring Data JPA
- 스프링 데이터 jpa
- 클린 아키텍처
- Jackson
- Linux
- JPA
- spring boot jwt
- leetcode
- 스프링 부트
- 스프링 부트 튜토리얼
- 스프링 부트 애플리케이션
- 스프링 부트 회원 가입
- Spring Boot Tutorial
- gRPC
- 헥사고날 아키텍처
- 함께 자라기 후기
- Java
- JSON
- intellij
- r
- 알고리즘
- proto3
- spring boot application
- @ManyToOne
- spring boot app
- 함께 자라기
- Spring Boot
- Spring Boot JPA
- 스프링부트
- QueryDSL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함