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.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note: You may assume k
is always valid, 1 ≤ k ≤ array's length
.
題意:求出數組中第 k
大的數字,不是第 k
大的獨特的數字,說明重複的數字也會佔據序次。
思路1:排序。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end(), greater<int>());
return nums[k - 1];
}
};
思路2:優先隊列(堆排序)。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, less<int>> pq;
for (const auto &i : nums) pq.push(i);
int ans;
for (int i = 0; i < k; ++i) {
ans = pq.top();
pq.pop();
}
return ans;
}
};
思路3:利用快速排序的分治策略。如果樞紐元是第 k
個數的話,直接返回,否則遞歸找到第 k
大的數。
class Solution {
public:
int partition(vector<int>& nums, int l, int r) { //劃分函數
if (l >= r) return l;
int t = nums[l];
while (l < r) {
while (l < r && nums[r] < t) --r;
nums[l] = nums[r];
while (l < r && nums[l] >= t) ++l;
nums[r] = nums[l];
}
nums[l] = t;
return l;
}
int select(vector<int>& nums, int l, int r, int k) {
if (l == r) return nums[l]; //遞歸邊界
int p = partition(nums, l, r); //劃分後主元的位置
int m = p - l + 1; //nums[p]在[l,r]中的位置
if (k == m) return nums[p]; //說明確定的主元位置正好是k
else if (k < m) return select(nums, l, p - 1, k); //第k大的數在左側
else return select(nums, p + 1, r, k - m); //第k大的數在右側
}
int findKthLargest(vector<int>& nums, int k) {
int kth = select(nums, 0, nums.size() - 1, k);
return kth;
}
};