Topic:
Find the kth largest element in an unsorted array. Note that it was the kth largest element in the sorted order and not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k are always valid, 1≤k≤array ' s length.
Links: http://leetcode.com/problems/kth-largest-element-in-an-array/
Exercises
To find the element K in the array, you can use Heap,radix sort or quick-select. But Quick-select's time compleixty is only O (n) on the amorized analysis. Here is the use of Java comes with the priority queue Min-heap to complete, is considered opportunistic. Two brushes to fill the radix sort and quick-select
Time Complexity-o (NLOGN), Space Complexity-o (k)
Public classSolution { Public intFindkthlargest (int[] Nums,intK) {//Use min oriented heap, store min value at top of the heap if(Nums = =NULL|| Nums.length = = 0) return0; Priorityqueue<Integer> PQ =NewPriorityqueue<integer>(k); for(inti = 0; i < nums.length; i++) { if(Pq.size () <k) Pq.offer (Nums[i]); Else { if(Nums[i] >Pq.peek ()) {Pq.poll (); //Remove minPq.offer (Nums[i]); } } } returnPq.poll (); }}
Reference:
https://leetcode.com/discuss/38336/solutions-partition-priority_queue-multiset-respectively
Https://leetcode.com/discuss/45627/ac-clean-quickselect-java-solution-avg-o-n-time
Https://leetcode.com/discuss/36913/solutions-java-having-worst-time-complexity-with-explanation
Https://leetcode.com/discuss/36991/java-quick-select
215. Kth largest Element in an Array