题解 | 第k轻的牛牛
第k轻的牛牛
https://www.nowcoder.com/practice/7676478b46794456b145e8e48b0e2763
import java.util.*;
public class Solution {
public int findKthSmallest (int[] weights, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k + 1, (o1, o2) -> (o2 - o1));
for (int w : weights) {
maxHeap.add(w);
if (maxHeap.size() == k + 1) {
maxHeap.poll();
}
}
return maxHeap.peek();
}
}