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.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Hide Tags Divide and Conquer Heap
This problem only came up with the method of using the heap to solve, did not think of how to use division to solve. But on the discuss, I saw a very detailed introduction to the use of the Division method, and gave a very concise code for the split section.
Solution one: Using the minimum heap
The problem can be solved by using the minimum heap or using the maximum heap, first of all using the least-heap solution. The topmost element in the smallest heap is the smallest in the heap, and we construct a minimum heap of size k, so the topmost element is the number K.
Using the number of first k in the array to form a minimum heap, then the top element of the heap is the number of K, and then from the number of k+1 to iterate through the array, if the traversed array element is less than the top element of the heap, if the traversed array element is larger than the top element of the heap, the heap top elements out of the heap The elements in the array that are larger than the top elements of the heap are then inserted into the heap, again forming the heap. This way, the top element of the array heap is traversed, which is the K-large element, which guarantees that the top element of the heap is the K-large element that goes to the traversed element when traversing to any element of the array.
Runtime:8ms
intFindkthlargest ( vector<int>& Nums,intk) {Make_heap (Nums.begin (), Nums.begin () +k,greater<int> ()); for(intI=k;i<nums.size (); i++) {intTop=nums.front ();if(Nums[i]>top) {Pop_heap (Nums.begin (), Nums.begin () +k,greater<int> ()); nums[k-1]=nums[i]; Push_heap (Nums.begin (), Nums.begin () +k,greater<int> ()); } }returnNums.front (); }
Solution Two: Using the maximum heap
Using the largest heap can also be solved, that is, the entire array of elements into a maximum heap, when the top of the heap is the largest element, the top element of the heap is continuously ejected k-1 times the top element of the heap is the number of k large. This solution may be more intuitive because the largest heap is built by default in the STL.
Runtime:8ms
int findKthLargest(vector<int>int k) { make_heap(nums.begin(), nums.end()); for (int i=1; i<k; i++){ pop_heap(nums.begin(), nums.end()); nums.pop_back(); } return nums[0];}
Solution three: Divide and conquer the method
The division of the law did not come out, but in the discuss saw a very detailed answer. I translated it into Chinese.
This solution is developed by the rapid sequencing.
In a quick sort, each iteration, we need to select a key element pivot and then split the array into three parts:
- Element less than pivot of key element
- element equal to pivot of key element
- element that is greater than the pivot of the key element
Now, take the [3,2,1,5,4,6] array as an example to analyze. Assuming that the leftmost element is selected as the key element pivot each time, in this case, it is 3, then we use 3 as pivot to divide the array into the 3 parts specified above, and the result is [1,2,3,5,4,6]. Now 3 is the 3rd element and we know it is also the 3rd small element.
Since the above segmentation is a smaller element than pivot is placed on the left side of the pivot, so pivot when pivot in the k-1 position is the K small element. Because this topic needs to find the element K, we can modify the segmentation process will be larger than the pivot element on the left side of K. Thus, after the partition is complete the array becomes [5,6,4,3,1,2], now 3 is the 4th largest element, and if we need to look for the 2nd largest element, we know it is on the left of 3, if we need the 5th large element, we know it is on the 3 right.
Now simply write out the flow of the algorithm:
- Initialize left to 0,right to Nums.size ()-1
- Split array, if Pivot is in the k-1 bit, return pivot
- If pivot is on k-1, update the position value of right for pivot
- Otherwise, the position value of left as pivot is updated.
- To repeat step 2
Runtime:8ms
intFindkthlargest ( vector<int>& Nums,intK) {intpivot=nums[0];intleft=0;intRight=nums.size ()-1; while(true) {intPos=partion (Nums,left,right);if(pos==k-1)returnNums[pos];if(pos<k-1) left=pos+1;Elseright=pos-1; } }intPartion ( vector<int>&nums,intBeginintEnd) {intleft=begin+1;intRight=end; while(Left<=right) {if(Nums[left]<nums[begin]&&nums[right]>nums[begin]) swap (nums[left],nums[right]);if(Nums[left]>=nums[begin]) left++;if(Nums[right]<=nums[begin]) right--; } swap (Nums[begin],nums[right]);returnRight }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode215:kth largest Element in an Array