Lintcode5 Kth Largest Element solution 题解

【题目描述】

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/

时间: 2024-10-12 02:33:18

Lintcode5 Kth Largest Element solution 题解的相关文章

Leetcode题解(3):L215/Kth Largest Element in an Array

L215: 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 as

215. Kth Largest Element in an Array (have better solution )

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333 } span.s1 { } span.s2 { font: 13.0px Menlo; color: #c7254e; background-color: #f9f2f4 } Find the kth largest element in an unsorted array. Note that it is the

【LeetCode】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 ≤ arr

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 le

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description 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 eleme

LeetCode 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

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'

【leetcode】Kth Largest Element in an Array (middle)☆

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. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i

LeetCode——Kth Largest Element in an Array

Description: 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. 你确定你不是在逗我? public class Solution { public