在未排序的數組中找到第 k 個最大的元素。請注意,你需要找的是數組排序後的第 k 個最大的元素,而不是第 k 個不同的元素。
示例 1:
輸入: [3,2,1,5,6,4] 和 k = 2
輸出: 5
示例 2:輸入: [3,2,3,1,2,4,5,5,6] 和 k = 4
輸出: 4
你可以假設 k 總是有效的,且 1 ≤ k ≤ 數組的長度。
具體的思路看這一篇文章:遞歸與分治 | 1:選擇算法/中位數 —— 例題:油井。此文是求第K個最小元素,而此題求第K個最大元素。只需要小小改動即可,思路一樣。
下面附上AC代碼:
void swap(int a[], int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
/* 將數組a的[s, e]範圍,按照與pivot的大小關係,劃分到pivot兩側 *
* 返回pivot最終的下標
* 注:pivot是隨機選取的 */
int RandomizedPartition(int a[], int s, int e) {
int pivot = a[e]; //取最後一個元素爲pivot來劃分
int i = s, j = e - 1;
while (i < j) {
while (a[i] >= pivot && i < e - 1)
i++;
while (a[j] <= pivot && j > s)
j--;
if (i < j)
swap(a, i, j);
}
if(a[i] > pivot) //所有元素都比pivot大,原序列不需要調整
return e;
else {
swap(a, i, e); //將pivot轉到合適位置
return i;
}
}
/* 找數組a[s, e]範圍內的第k小元素 */
int RandomizedSelect(int a[], int s, int e, int k) {
int pivot_index = RandomizedPartition(a, s, e); //按照與pivot的大小關係,劃分到pivot兩側。返回pivot最終的下標
int num = pivot_index - s + 1; //pivot(包括在內)左側的元素個數
if (num == k)//第k小元素恰好是pivot
return a[pivot_index];
else if (k < num) //在pivot左邊找
return RandomizedSelect(a, s, pivot_index - 1, k);
else //在pivot右邊找
return RandomizedSelect(a, pivot_index + 1, e, k - num);
}
int findKthLargest(int* nums, int numsSize, int k){
return RandomizedSelect(nums, 0, numsSize - 1, k);
}