题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
public int findKth(int[] a, int n, int k) { PriorityQueue<Integer> q = new PriorityQueue<>(); for(int x:a){ q.offer(x); if(q.size()>k){ q.poll(); } } return q.peek(); }
找大的元素用小根堆,找小的元素用大根堆