您的位置:首页 > 其它

LeetCode 215. Kth Largest Element in an Array

2018-02-16 18:11 302 查看
https://leetcode.com/problems/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.

Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.

数组排序题。

第一种解法,最简单的用STL sort排序,因为是升序排列,所以nums[n - K]就是第K大的元素。

sort - C++ Reference
http://www.cplusplus.com/reference/algorithm/sort/
第二种解法,用快排中的分治方法。因为快排中每一趟排序之后能找出第pos大的元素,所以K趟排序就能找出第K大的元素。按照从大到小排序,一趟排序之后,如果pos < K,那么Kth在pos右边区间,反之则在pos左边区间。时间复杂度O(N),最坏情况在已经排好序的情况下为O(N^2)。

[Data Structure & Algorithm] 八大排序算法 - Poll的笔记 - 博客园
http://www.cnblogs.com/maybe2030/p/4715042.html#_label5
第三种解法可以用STL priority_queue。因为优先队列实际上用到了最大堆,默认情况下top返回最大元素,K次循环就可以找到第K大的元素。

priority_queue - C++ Reference
http://www.cplusplus.com/reference/queue/priority_queue/
第四种解法可以用STL multiset。多元集合默认情况下按升序排列,所以维护一个K个元素的集合,begin()就是第K大的元素。

multiset - C++ Reference
http://www.cplusplus.com/reference/set/multiset/
第五种解法可以用堆排序。

Kth Largest Element in an Array - LeetCode
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60309/4-C++-Solutions-using-Partition-Max-Heap-priority_queue-and-multiset-respectively
//
//  main.cpp
//  LeetCode
//
//  Created by Hao on 2017/3/16.
//  Copyright © 2017年 Hao. All rights reserved.
//

#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;

class Solution {
public:
// STL sort
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end()); // STL sort in ascending order
return nums.at(nums.size() - k);
}

// Divide and Conquer
int findKthLargest2(vector<int>& nums, int k) {
int l = 0, r = nums.size() - 1;

while (true) {
int pos = partition(nums, l, r); // one time quick sort

if (pos == k - 1) // the k-th largest key
return nums.at(k - 1);
else if (pos < k - 1) // in the right partition of pos
l = pos + 1;
else // in the left partition of pos
r = pos - 1;
}
}

int partition(vector<int> & nums, int left, int right) {
int i = left, j = right, pivot = nums.at(left); // pivot key

while (i < j) {
while (i < j && nums.at(j) <= pivot) -- j; // scan from right to left
if (i < j) nums.at(i ++) = nums.at(j); // place nums[j]

while (i < j && nums.at(i) >= pivot) ++ i; // scan from left to right
if (i < j) nums.at(j --) = nums.at(i); // place nums[i]
}

nums.at(i) = pivot; // place pivot key - the (i+1)-th largest key

return i;
}

// priority_queue
int findKthLargest3(vector<int>& nums, int k) {
priority_queue<int> pq(nums.begin(), nums.end());

for (int i = 0; i < k - 1; i ++)
pq.pop(); // remove k-1 largest elements

return pq.top(); // Kth largest element
}

// multiset
int findKthLargest4(vector<int>& nums, int k) {
multiset<int>   mset;

for (int i = 0; i < nums.size(); ++ i) {
mset.insert(nums.at(i));

if (mset.size() > k)
mset.erase(mset.begin()); // erase the element < Kth largest element
}

return *mset.begin(); // Kth largest element
}
};

int main(int argc, char* argv[])
{
Solution    testSolution;

vector<vector<int>>     aInputs = {{3,2,1,5,6,4}, {1,2,3,4,5}, {10,9,8,7,6}};
vector<int>             kInputs = {2, 4, 2};
int                     result;

/*
5
2
9
*/
for (auto i = 0; i < aInputs.size(); ++ i) {
result = testSolution.findKthLargest(aInputs[i], kInputs[i]);
cout << result << endl;
result = testSolution.findKthLargest2(aInputs[i], kInputs[i]);
cout << result << endl;
result = testSolution.findKthLargest3(aInputs[i], kInputs[i]);
cout << result << endl;
result = testSolution.findKthLargest4(aInputs[i], kInputs[i]);
cout << result << endl;
}

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: