[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 ...
随机推荐
- linux常见基础问题
1,32位与64位的区别,怎么查看系统版本? 32位相比于64位处理速度更慢一些,64位同样也比32位更占内存.用户体验上没有区别:用uname -a 查看系统版本信息 2,swap分区的作用是什么 ...
- docker 基本指令
sudo docker info 查看docker状态. jiqing@ThinkPad:~$ sudo docker info [sudo] password for jiqing: Contain ...
- POJ1808 平方(二次)同余方程
POJ1808 给定一个方程 x*x==a(mod p) 其中p为质数 判断是否有解 程序中 MOD_sqr()返回整数解 无解返回-1 数学证明略 #include<iostream> ...
- oracle 定时器调用存储过程
转载请说明出处:http://t22011787.iteye.com/blog/1112745 一.查询系统中的job,可以查询视图 --相关视图 select * from dba_jobs; se ...
- 杂项-公司:Sun
ylbtech-杂项-公司:Sun Sun Microsystems是IT及互联网技术服务公司(已被甲骨文收购)Sun Microsystems 创建于1982年.主要产品是工作站及服务器.1986年 ...
- visual studio使用dos命令在生成项目时复制文件到指定目录
本人使用软件:vs2015 拷贝“项目1”的 bin目录 下, 项目配置的名称(“Release”,“Debug”)目录下,所有内容到“项目2”输出目录(存在直接覆盖): xcopy $(Soluti ...
- 理解 Java 构造函数不可以继承
参考来源:http://www.52bowen.com/a/2604620.html
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- centos 7 中防火墙的关闭问题
新安装的centos 7 发现有些程序端口是关闭的,想到了防火墙和selinux selinx 好关闭 /etc/sysconfig/selinux 中 追加 SELINUX=disabled 防火 ...
- layer父页获取弹出层输入框里面的值
主要是因为修改功能,原来页面填写数据如图 改为 其中点击填写明细弹出框 填写完毕后点击确认返回,同事这里因为她是存的多表,所以点击确认就直接保存数据了,改的这个功能原本保存是整体保存,我就不想改原来的 ...