[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 ...
随机推荐
- YTU 2690: 用双重循环实现小九九
2690: 用双重循环实现小九九 时间限制: 1 Sec 内存限制: 128 MB 提交: 848 解决: 573 题目描述 小九九是我们小时候常背的的乘法算术法则,现在用双重循环来实现小九九 1 ...
- js验证手机号,身份证,车牌号验证
js验证手机号 <input type="text" class="identificationno"> // 身份证号码为15位或者18位,15 ...
- python-----列表生成式和列表生成器表达
列表表达式: 程序一: 常规写法: L = [] for x in range(1, 11): L.append(x * x) print(L) #[1, 4, 9, 16, 25, 36, 49, ...
- CentOS下网卡启动、配置等ifcfg-eth0教程
步骤1.配置/etc/sysconfig/network-scripts/ifcfg-eth0 里的文件. CentOS6.4 下的ifcfg-eth0的配置详情: [root@Jeffery]# v ...
- bzoj 1059: [ZJOI2007]矩阵游戏【匈牙利算法】
注意到怎么换都行,但是如果把某个黑方块用在对角线上,它原来所在的行列的的黑方块就都不能用 所以要选出n组不重的行列组合,这里用匈牙利算法做二分图匹配即可(用了时间戳优化) #include<io ...
- 启动VMware环境下的Linux操作系统,添加新分区
启动VMware环境下的Linux操作系统,添加新分区,需要root账号身份. 3.1 [fdisk -l] 最大分区为/dev/sda3,说明新创建的分区将会是sda4 3.2 输入[fdisk / ...
- JavaScript--DOM访问子结点childNodes
访问子结点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...
- JS在即将离开当前页面(刷新或关闭)时触发事件
// onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发 window.onbeforeunload = function () { return /^\#\/ipinfo/.t ...
- datagrid上面的查询按钮设置了,但是分页工具栏不显示
原因:查询的linkbutton没有放在toolbar里. <script type="text/javascript"> $(function(){ $('#dg') ...
- can't set blob value on that column
MySQL_Prepared_Statement::setBlob: can't set blob value on that column, MySQL error code:0, SQLState ...