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的更多相关文章

  1. Lintcode: Kth Largest Element 解题报告

    Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 【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 ...

  6. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  7. 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 ...

  8. 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 ...

  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 ...

随机推荐

  1. 扩展欧几里得模板&逆元求法

    拓展欧几里得: 当 gcd ( a , b )= d 时,求绝对值和最小的 x , y 使得 x * a + y * b = d : d = gcd ( a , b ) = gcd ( b , a m ...

  2. Android6.0源码分析之录音功能(一)【转】

    本文转载自:http://blog.csdn.net/zrf1335348191/article/details/54949549 从现在开始一周时间研究录音,下周出来一个完整的博客,监督,激励!!! ...

  3. 【Silverlight】Bing Maps学习系列(四):使用图钉层(Pushpin layer)及地图图层(MapLayer)(转)

    [Silverlight]Bing Maps学习系列(四):使用图钉层(Pushpin layer)及地图图层(MapLayer) 如果我们需要在Bing Maps中加入一个小图钉标记,该如何实现了? ...

  4. POJ2187 Beauty Contest (旋转卡壳算法 求直径)

    POJ2187 旋转卡壳算法如图 证明:对于直径AB 必然有某一时刻 A和B同时被卡住 所以旋转卡壳卡住的点集中必然存在直径 而卡壳过程显然是O(n)的 故可在O(n)时间内求出直径 凸包具有良好的性 ...

  5. 微信小程序setData的使用,通过[...]进行动态key赋值

    首先先介绍一下微信小程序Page.prototype.setData(Object data, Function callback)的讲解: setData函数用于将数据从逻辑层发送到视图层(异步), ...

  6. ----堆栈 STL 函数库 ----有待补充

    #include<cstdio> #include<string> #include<vector> #include<iostream> using ...

  7. SPFA+Dinic HDOJ 5294 Tricks Device

    题目传送门 /* 题意:一无向图,问至少要割掉几条边破坏最短路,问最多能割掉几条边还能保持最短路 SPFA+Dinic:SPFA求最短路时,用cnt[i]记录到i最少要几条边,第二个答案是m - cn ...

  8. Axis通过wsdd部署Web Service

    axis网上的教程很多,不过搜来搜去,总是只有那么几篇.仔细看了一下那几篇文章,都感觉到不是自己想要的,所以自己整理了一篇分享一下. 本文介绍axis应用的一个小例子,没有麻烦的命令行操作,只需照下面 ...

  9. [转]MySQL存储过程

    转自:http://www.cnblogs.com/exmyth/p/3303470.html 14.1.1 创建存储过程 MySQL中,创建存储过程的基本形式如下: CREATE PROCEDURE ...

  10. Intellij使用心得(四) -- 导入Eclipse的代码格式化文件

    https://my.oschina.net/flashsword/blog/137598