215. Kth largest Element in an Array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.