LeetCode215. 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.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.


std::sort(),reverse().O(nlogn)

1
2
3
4
5
6
7
8
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
reverse(nums.begin(),nums.end());
return nums[k-1];
}
};

Partition.O(n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <vector>
using namespace std;

class Solution {
public:
int findKthLargest(vector<int>& nums, int k)
{
if(nums.empty()||k>nums.size())
return 0;

k=nums.size()-k;//从小到大排列后的k
QuickSort_Recur(nums,0,nums.size()-1);

return nums[k];
}
void QuickSort_Recur(vector<int>& nums, int low, int high)
{
if(low<high)
{
int index = Partition(nums,low,high);//找到一个基准,并将所有小于基准的数放在基准左边。
QuickSort_Recur(nums,low,index-1);
QuickSort_Recur(nums,index+1,high);
}
}

int Partition(vector<int>& nums,int start,int end)
{
if(start<0||end>=nums.size())
return 0;

int pivot = random(start,end);
swap(&nums[pivot],&nums[end]);//把基准移到尾部

int left = start -1;//最左边原地划分一个子集
for(int i =start;i<end;i++)
{
if(nums[i]<nums[end])//如果小于基准就放入子集中
{
left++;
if(left!=i)
swap(&nums[i],&nums[left]);
}
}

left++;
swap(&nums[left],&nums[end]);//将最尾部的基准放在左子集的下一个位置

return left;//返回基准
}
int random(int min,int max)
{
int random =rand()%(max-min+1) +min;
return random;
}
void swap(int *in1,int* in2){
int temp=*in1;
*in1=*in2;
*in2=temp;
}
};

Heapsort

max-heap:将N个元素塞入一个最大堆,将堆顶元素删除k-1次。堆顶就是要找的第K大的元素。
min-heap:将K个最大元素塞入一个最小堆。堆顶就是要找的第K大的元素。
在STL中。priority_queuemultiset可以实现最大堆/最小堆。

using priority_queue

min-heap

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int num : nums) {
pq.push(num);
if (pq.size() > k) {
pq.pop();
}
}
return pq.top();
}
};

max-heap

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> pq(nums.begin(), nums.end());
for (int i = 0; i < k - 1; i++) {
pq.pop();
}
return pq.top();
}
};

using multiset(rbt)

min-heap

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
multiset<int> mset;
for (int num : nums) {
mset.insert(num);
if (mset.size() > k) {
mset.erase(mset.begin());
}
}
return *mset.begin();
}
};

max-heap

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
multiset<int, greater<int>> mset(nums.begin(), nums.end());
for (int i = 0; i < k - 1; i++) {
mset.erase(mset.begin());
}
return *mset.begin();
}
};

STL的nth_element和partial_sort(没掌握)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), greater<int>());
return nums[k - 1];
}
};
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
partial_sort(nums.begin(), nums.begin() + k, nums.end(), greater<int>());
return nums[k - 1];
}
};

请注意两个内置函数的第二个参数中的1之差。

Thanks for Support.