Kth Largest Element in an Array
来源:互联网 发布:java自动包装功能 编辑:程序博客网 时间:2024/06/10 06:27
题目
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.
哇,这个题其实可以用暴力求解,用快排后在返回nums[k-1];就可以了;
class Solution {public: int findKthLargest(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); return nums[k - 1]; }};
分部
但是显然这个不是我们做题的目的,根据分治算法的理念,可以用课上老师讲过的方法将数组划分:
取任意值(默认为左边第一个)pivot;然后将数组分为三部分
<pivot;=pivot;>pivot
然后根据K和最后pivot的位置再决定在哪个子数组中寻找,直到找到
class Solution { public: int partition(vector<int>& nums, int left, int right) { int pivot = nums[left]; int l = left + 1, r = right; while (l <= r) { if (nums[l] < pivot && nums[r] > pivot) swap(nums[l++], nums[r--]); if (nums[l] >= pivot) l++; if (nums[r] <= pivot) r--; } swap(nums[left], nums[r]); return r; } int findKthLargest(vector<int>& nums, int k) { int left = 0, right = nums.size() - 1; while (true) { int pos = partition(nums, left, right); if (pos == k - 1) return nums[pos]; if (pos > k - 1) right = pos - 1; else left = pos + 1; } }};
答案中最强的分部
class Solution {public: int partition(vector<int> &nums, int lo, int hi) { swap(nums[lo], nums[lo + rand() % (hi - lo + 1)]); int pivot = nums[lo]; while (lo < hi) { while ((lo < hi) && pivot <= nums[hi]) { --hi; } nums[lo] = nums[hi]; while ((lo < hi) && nums[lo] <= pivot) { ++lo; } nums[hi] = nums[lo]; } nums[lo] = pivot; return lo; } int findKthLargest(vector<int>& nums, int lo, int hi, int k) { if(hi-lo<1){ return nums[lo]; } int p = partition(nums, lo, hi); if(p == k){ return nums[p]; } if(p > k){ return findKthLargest(nums,lo, p-1,k); }else{ return findKthLargest(nums,p+1, hi,k); } } int findKthLargest(vector<int>& nums, int k) { k = nums.size()-k; return findKthLargest(nums,0,nums.size()-1,k); }};
priority
使用priority队列,让队列大小保持为nums.size() - k + 1
class Solution {public: int findKthLargest(vector<int>& nums, int k) { priority_queue<int> pq; for(auto n : nums){ pq.push(n); if(pq.size() > nums.size() - k + 1) pq.pop(); } return pq.top(); }};
阅读全文
0 0
- Kth Largest Element in an Array
- Leetcode Kth Largest Element in an Array
- Kth Largest Element in an Array
- 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] Kth Largest Element in an Array
- Kth Largest Element in an Array
- Kth Largest Element in an Array
- leetcode--Kth Largest Element in an Array
- Kth Largest Element in an Array
- 215.Kth Largest Element in an Array
- #leetcode#Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- LeetCode215:Kth Largest Element in an Array
- Kth Largest Element in an Array
- 初识.net界面程序(15)--鼠标事件
- void及void指针含义的深刻解析
- 数据结构之线性表顺序存储
- 重要!非常重要的提示,必看!!!
- 美陆军发布《网络战和电子战FM(战地手册)3-12》报告
- Kth Largest Element in an Array
- HTML与XHTML的区别
- Kotlin学习过程 三 Kotlin基础语法二
- 论文阅读笔记一(Recognizing Keystrokes Using WiFi Devices)
- windows下git识别大小写配置
- CMTime
- HDU5698(排列组合 C(a,b)modP 模板 )
- webView本地缓存
- tensorflow用于数据增强