215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length
题目含义:求给定的数组中,前k大个数
1 public int findKthLargest(int[] nums, int k) { 2 // final int N = nums.length; 3 // Arrays.sort(nums); 4 // return nums[N - k]; 5 PriorityQueue<Integer> pq = new PriorityQueue<>(); 6 for (int i = 0; i < nums.length; i++) { 7 pq.offer(nums[i]); 8 if (pq.size() > k) pq.poll(); 9 } 10 return pq.peek(); 11 }