Kth Largest in N Arrays
Find K-th largest element in N arrays.
Notice
You can swap elements in the array.
Example
Inn=2arrays[[9,3,2,4,7],[1,2,3,4,8]], the 3rd largest element is7.
Inn=2arrays[[9,3,2,4,8],[1,2,3,4,2]], the 1st largest element is9, 2nd largest element is8, 3rd largest element is7and etc.
public int KthInArrays(int[][] arrays, int k) {
if(arrays == null || arrays.length == 0) {
return 0;
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, Collections.reverseOrder());
for(int i = 0; i < arrays.length; i++) {
for(int j = 0; j < arrays[i].length; j++) {
maxHeap.offer(arrays[i][j]);
}
}
for(int i = 0; i < k - 1; i++) {
maxHeap.poll();
}
return maxHeap.peek();
}