[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 ...
随机推荐
- luogu1314 聪明的质检员
题目大意 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1 到 n 逐一编号,每个矿石都有自己的重量 wi 以及价值 vi.检验矿产的流程是: 1.给定 m 个区 ...
- 2015-2016 ACM-ICPC Pacific Northwest Regional Contest (Div. 2) S Surf
SurfNow that you've come to Florida and taken up surng, you love it! Of course, you've realized that ...
- FreeMarker:目录
ylbtech-FreeMarker:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://yl ...
- 33. Extjs中的tree节点的操作
转自:https://blog.csdn.net/masterShaw/article/details/51354351?utm_source=blogkpcl9 ext 树节点操作 tree ...
- Rails 插入代码与注释
醉了醉了,在原来那个表格最后加了然后更新博客,然后最后写的内容就没了.来来回回试了n次都一样.不得已新开一个 插入代码 <% ... %> 打印值 <%= ... %&g ...
- poj 1286 Necklace of Beads【polya定理+burnside引理】
和poj 2409差不多,就是k变成3了,详见 还有不一样的地方是记得特判n==0的情况不然会RE #include<iostream> #include<cstdio> us ...
- idea下载安装指南
官网地址 https://www.jetbrains.com/idea/ 点击download 有收费版本和社区免费版.我下载了免费的. 有zip和exe两个版本.我先下载了zip绿色版,发现用不了. ...
- 洛谷P5398 [Ynoi2018]GOSICK(二次离线莫队)
题面 传送门 题解 维包一生推 首先请确保您会二次离线莫队 那么我们现在的问题就是怎么转移了,对于\(i\)和前缀\([1,r]\)的贡献,我们拆成\(b_i\)和\(c_i\)两部分,其中\(b_i ...
- 1.2Hello, World!的大小
描述 还记得在上一章里,我们曾经输出过的“Hello, World!”吗? 它虽然不是本章所涉及的基本数据类型的数据,但我们同样可以用sizeof函数获得它所占用的空间大小. 请编程求出它的大小,看看 ...
- 2017杭电多校第六场1008 Kirinriki
传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...