[Algorithm] 5. Kth Largest Element
Description
Find K-th largest element in an array.
Example
In array [9,3,2,4,8]
, the 3rd largest element is 4
.
In array [1,2,3,4,5]
, the 1st largest element is 5
, 2nd largest element is 4
, 3rd largest element is 3
and etc.
Challenge
O(n) time, O(1) extra memory.
Notice
You can swap elements in the array
Answer
/**
* @param n: An integer
* @param nums: An array
* @return: the Kth largest element
*/
int kthLargestElement(int n, vector<int> &nums) {
// find out the kth largest number in an array.
int temp=;
for(int i=; i<=n; i++)
{
for(int j=; j<nums.size()-i; j++)
{
// Swap the larger number to the end.
if(nums[j] >= nums[j+])
{
temp = nums[j];
nums[j] = nums[j+];
nums[j+] = temp;
}
}
} return nums[nums.size()-n];
}
Better Solution
/**
* @param n: An integer
* @param nums: An array
* @return: the Kth largest element
*/ int quick_sort(vector<int> &nums, int l, int r)
{
int key=nums[l];
while(l<r)
{
while( (nums[r] >= key) && (l<r) ) r--;
swap(nums[l], nums[r]); while( (nums[l] <= key) && (l<r) ) l++;
swap(nums[l], nums[r]);
} return l;
} int kthLargestElement(int n, vector<int> &nums) {
// Using qsort method to find the kth largest number.
if(n>nums.size() || n== || nums.size()==) return -; int left=, right=nums.size()-, k=nums.size()-n, pivot=; while(true){
pivot = quick_sort(nums, left, right);
if(pivot==k){
return nums[k];
}else if(pivot<k){
left=pivot+;
right=nums.size()-;
}else{
left=;
right=pivot-;
}
}
}
Best Solution
/**
* @param n: An integer
* @param nums: An array
* @return: the Kth largest element
*/
int kthLargestElement(int n, vector<int> &nums) {
// Using priority queue to solve it.
if(n>nums.size() || n== || nums.size()==) return -; priority_queue<int, vector<int>, greater<int>> pri_queue;
for(int i=; i<n; i++)
{
pri_queue.push(nums[i]);
}
for(int i=n; i<nums.size(); i++)
{
if( nums[i] > pri_queue.top() )
{
pri_queue.pop();
pri_queue.push(nums[i]);
}
} return pri_queue.top();
}
Tips
It is a good way to use STL container(priority queue) to solve the problem.
[Algorithm] 5. Kth Largest Element的更多相关文章
- Lintcode: Kth Largest Element 解题报告
Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 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 so ...
- Java for 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 so ...
- 【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 so ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- Kth Largest Element in an Array - LeetCode
examination questions Find the kth largest element in an unsorted array. Note that it is the kth lar ...
- Kth Largest Element in an Array
Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...
- 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 so ...
随机推荐
- Codeforces Round #100 A. New Year Table
A. New Year Table time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- android压力测试命令monkey详解【转】
本文转载自:http://www.jb51.net/article/48557.htm 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-29我要评论 这篇文章主要介绍了android ...
- securecrt中vim行号下划线问题及SecureCRT里root没有高亮的设置,修改linux终端命令行颜色
背景:在用raspberry用SecureCRT下的vim打开文件时出现用set nu时行有下划线,于是找了下解决办法,如下:vim行号下划线问题在vim中发现开启显示行号(set number) ...
- bzoj2060
树形dp dp[x][0]表示x点父亲没选,dp[x][1]表示x点父亲选了,然后dp[x][0]=max(sigma(dp[c[x]][0]),sigma(dp[c[x]][1])) dp[x][1 ...
- 【154】C#打包程序成安装包
参考0:用C#写完程序怎么用C#打包成安装程序setup自己做的图文说明示例 参考1:解决“默认公司名称” C#打包应用安装后,显示“默认公司名称”,想问问通过哪里可以修改??? 参考2:解决“添加卸 ...
- KeepAlived的介绍
KeepAlived介绍 keepalived keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层.第4层和第7层交换. Keepalived的 ...
- JAVA的引用类型变量(C/C++中叫指针)
任何变量只要存在内存中,就需要有一个唯一的编号标识这个变量在内存中的位置,而这个唯一的内存编号就是内存地址,内存地址就是所谓的指针!
- (进制)51NOD 1057 N的阶乘
输入N求N的阶乘的准确值. Input 输入N(1 <= N <= 10000) Output 输出N的阶乘 Input示例 5 Output示例 120解:这其实是MOD进制,将一个 ...
- C# 树次结构的数据
public static void CreateTree(TreeView tv) { ///获取层次结构的数据 Tree tree = new Tree(); DataSet ds = tree. ...
- hdu1166 敌兵布阵(树状数组)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...