题目:
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.
用Quick Sort的divide-and-conquer法,或者用Priority Queue (Max Heap) 数据结构,注意Java和Python都是最小堆,需要转换一下。
C++版解法一:
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
return nums[returnXth(nums, 0, nums.size() - 1, k)];
}
int returnXth(vector<int>& nums, int head, int tail, int k) {
int start = head;
int end = tail;
srand((unsigned)time(0));
int pivot = (rand() % (end + 1 - start)) + start;
swap(nums[start], nums[pivot]);
pivot = start;
while (start <= end) {
while (start <= end && nums[end] >= nums[pivot]) end--;
if (start <= end && nums[end] < nums[pivot]) {
swap(nums[pivot], nums[end]);
pivot = end;
end--;
}
while (start <= end && nums[start] <= nums[pivot]) start++;
if (start <= end && nums[start] > nums[pivot]) {
swap(nums[start], nums[pivot]);
pivot = start;
start++;
}
}
if (pivot > nums.size() - k) {
pivot = returnXth(nums, head, pivot - 1, k);
}
else if (pivot < nums.size() - k) {
pivot = returnXth(nums, pivot + 1, tail, k);
}
return pivot;
}
};
C++版解法二:
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> a;
for(int i = 0; i < nums.size(); i++) {
a.push(nums[i]);
}
for(int i = 0; i < k - 1; i++)
a.pop();
return a.top();
}
};
Java版解法一:
public class Solution {
public int findKthLargest(int[] nums, int k) {
return nums[findXth(nums, 0, nums.length-1, k)];
}
public int findXth(int[] nums, int head, int tail, int k) {
int begin = head;
int end = tail;
int pivot = begin;
int temp;
while(begin <= end) {
while(begin <= end && nums[end] >= nums[pivot]) end--;
if(begin <= end && nums[end] < nums[pivot]) {
temp = nums[pivot];
nums[pivot] = nums[end];
nums[end] = temp;
pivot = end;
end--;
}
while(begin <= end && nums[begin] <= nums[pivot]) begin++;
if(begin <= end && nums[begin] > nums[pivot]) {
temp = nums[pivot];
nums[pivot] = nums[begin];
nums[begin] = temp;
pivot = begin;
begin++;
}
}
if(pivot > nums.length - k)
pivot = findXth(nums, head, pivot-1, k);
else if(pivot < nums.length - k)
pivot = findXth(nums, pivot+1, tail, k);
return pivot;
}
}
Java版解法二:
import java.util.PriorityQueue;
public class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<>();
for(int i = 0; i < nums.length; i++)
heap.add(nums[i]);
for(int i = 0; i < nums.length - k; i++)
heap.poll();
return heap.peek();
}
}
Python版解法一:
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {integer}
def findKthLargest(self, nums, k):
return nums[self.findXth(nums, 0, len(nums)-1, k)]
def findXth(self, nums, head, tail, k):
begin = head
end = tail
pivot = head
while begin <= end:
while begin <= end and nums[end] >= nums[pivot]:
end -= 1
if begin <= end and nums[end] < nums[pivot]:
nums[end], nums[pivot] = nums[pivot], nums[end]
pivot = end
end -= 1
while begin <= end and nums[begin] <= nums[pivot]:
begin += 1
if begin <= end and nums[begin] > nums[pivot]:
nums[begin], nums[pivot] = nums[pivot], nums[begin]
pivot = begin
begin += 1
if pivot > len(nums) - k:
pivot = self.findXth(nums, head, pivot-1, k)
elif pivot < len(nums) - k:
pivot = self.findXth(nums, pivot+1, tail, k)
return pivot
Python版解法二:
import heapq
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @return {integer}
def findKthLargest(self, nums, k):
data = []
for i in nums:
heapq.heappush(data, -i)
for i in range(k-1):
heapq.heappop(data)
return -heapq.heappop(data)