leetcode 215. Kth Largest Element in an Array
来源:互联网 发布:数据挖掘 需要学c 编辑:程序博客网 时间:2024/05/23 23:40
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 sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
来源: https://leetcode.com/problems/kth-largest-element-in-an-array/description/
class Solution {public: int __selection(vector<int>& arr, int lo, int hi, int k){ if (lo==hi) return arr[lo]; //partition之后,arr[p]的正确位置就在索引p上 int p=__partition(arr, lo, hi); if (k==p) return arr[p]; else if(k<p) //如果k<p,只需要在arr[lo:p-1]之间找到第k小的元素即可 return __selection(arr, lo, p-1, k); else //如果k>p, 只需要在arr[p+1, hi]之间找到第k小的元素即可 return __selection(arr, p+1,hi, k); } int __partition(vector<int>& arr, int lo, int hi){ int p=rand()%(hi-lo+1)+lo; swap(arr[lo],arr[p]); //[lo+1:j] < p; int j=lo;// T v=arr[lo]; for (int i = lo+1 ; i <= hi ; ++i) { if (arr[i] <arr[lo]) swap( arr[i], arr[++j]); } swap(arr[j],arr[lo]); return j; } int findKthLargest(vector<int>& nums, int k) { int n=nums.size(); return __selection(nums, 0, n-1, n-k); } };
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 sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
来源: https://leetcode.com/problems/kth-largest-element-in-an-array/description/
class Solution {public: int __selection(vector<int>& arr, int lo, int hi, int k){ if (lo==hi) return arr[lo]; //partition之后,arr[p]的正确位置就在索引p上 int p=__partition(arr, lo, hi); if (k==p) return arr[p]; else if(k<p) //如果k<p,只需要在arr[lo:p-1]之间找到第k小的元素即可 return __selection(arr, lo, p-1, k); else //如果k>p, 只需要在arr[p+1, hi]之间找到第k小的元素即可 return __selection(arr, p+1,hi, k); } int __partition(vector<int>& arr, int lo, int hi){ int p=rand()%(hi-lo+1)+lo; swap(arr[lo],arr[p]); //[lo+1:j] < p; int j=lo;// T v=arr[lo]; for (int i = lo+1 ; i <= hi ; ++i) { if (arr[i] <arr[lo]) swap( arr[i], arr[++j]); } swap(arr[j],arr[lo]); return j; } int findKthLargest(vector<int>& nums, int k) { int n=nums.size(); return __selection(nums, 0, n-1, n-k); } };
阅读全文
0 0
- Leetcode Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- [LeetCode] Kth Largest Element in an Array
- [leetcode] Kth Largest Element in an Array
- leetcode--Kth Largest Element in an Array
- #leetcode#Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- 【Leetcode】Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- Kth Largest Element in an Array -- leetcode
- [Leetcode]Kth Largest Element in an Array
- [LeetCode]Kth Largest Element in an Array
- *LeetCode-Kth Largest Element in an Array
- LeetCode----Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- leetcode-Kth Largest Element in an Array
- LeetCode -- Kth Largest Element in an Array
- PHP之抽象类、接口
- Could not initialize class net.sf.json.util.JSONUtils
- 剑指offer 数组
- 前端高性能计算之一:WebWorkers
- 简单聊聊Android Architecture Componets
- leetcode 215. Kth Largest Element in an Array
- 路由协议攻击
- bootstrap日期插件daterangepicker的使用
- NOI2012 迷失游乐园 期望+树形dp+基环外向树
- BZOJ1700 DP
- 小美的桌号:DHCP协议
- 从头到尾彻底理解傅里叶变换算法(下)
- [初学笔记]matlab的具体类型图形命令函, bar条形图
- 关于帧率