美文网首页Lintcode程序员
Lintcode5 Kth Largest Element so

Lintcode5 Kth Largest Element so

作者: 代码码着玩 | 来源:发表于2017-03-18 14:53 被阅读26次

【题目描述】

Find K-th largest element in an array.

Notice:You can swap elements in the array

在数组中找到第k大的元素

注意:你可以交换数组中的元素的位置

【题目链接】

http://www.lintcode.com/en/problem/kth-largest-element/

【题目解析】

sort的方法:一开始看到这道题肯定觉得很简单,只要sort一下,然后return特定index的value就可以了,但是sort的time complexity至少是O(nlogn)

Quick Select:这个是由quick sort演化而来,用到了partition的部分,每次选一个pivot,小于它的放左边,大于它的放右边。

用Quick Sort的divide-and-conquer法,或者用Priority Queue (Max Heap) 数据结构,注意Java和Python都是最小堆,需要转换一下。

【题目答案】

http://www.jiuzhang.com/solutions/kth-largest-element/

相关文章

网友评论

    本文标题:Lintcode5 Kth Largest Element so

    本文链接:https://www.haomeiwen.com/subject/qzbunttx.html