1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public int findKthLargest(int[] nums, int k) { int len = nums.length; PriorityQueue<Integer> queue = new PriorityQueue<>(k,Comparator.comparingInt(a -> a)); for (int i = 0; i <len ; i++) { if (queue.size()<k){ queue.offer(nums[i]); }else { if (nums[i]>queue.peek()){ queue.poll(); queue.offer(nums[i]); } } } return queue.peek(); }
|