Given an integer array nums
and an integer k
, return the kth
largest element in the array.
Note that it is the kth
largest element in the sorted order, not the kth
distinct element.
Can you solve it without sorting?
요구사항: 정렬없이 nums 배열에서 k번째로 큰 수를 구하시오.
answer = 0
pq = 우선순위 큐 생성
for (nums의 원소들 num):
pq에 -1 * num을 추가한다
for k번 반복:
answer = pq에서 제거한다
return answer
import java.util.*;
class Solution {
public int findKthLargest(int[] nums, int k) {
int answer = 0;
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (var e: nums) {
pq.add(-e);
}
for (int i = 0; i < k; i++) {
answer = pq.poll();
}
return -answer;
}
}