此文为 LeetCode 215. Kth Largest Element in an Array 的题解。
题意
在一个未排序的数组中,寻找第k大的元素。
分析
这个题目其实很简单,但是踩了一些坑,所以记录一下:
首先是用PriorityQueue,然后poll k次即可(5 ms,37.5 MB)。但是属于API caller,略去不表。
其次就是堆排序,很久不写了,第一次把堆排序写成了选择排序,导致时间很长:101 ms,37.2 MB。
后来复习了下堆排序,写出来了(2 ms,36.6 MB):
// from https://www.robberphex.com/kth-largest-element-in-an-array/… Read the rest