Lintcode: Kth largest Element, lintcodekth

Source: Internet
Author: User
Tags lintcode

Lintcode: Kth largest Element, lintcodekth

Find K-th largest element in an array.NoteYou can swap elements in the arrayExampleIn array [9,3,2,4,8], the 3rd largest element is 4In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.ChallengeO(n) time, O(1) space

With the improved Quicksort partition, the time complexity of O (n) can be reached without extra space.

See http://en.wikipedia.org/wiki/Quickselect#Time_complexity

Quickselect uses the same overall approach as quicksort, choosing one element as a partition and partitioning the data in two based on the partition, accordingly as less than or greater than the partition. however, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side-the side with the element it is searching. this variable CES the average complexity from O (NLogN) (In quicksort) to O (N) (In quickselect ).

Note the following code:

1. Row 8th Kth largest element = len-K + 1 th smallest element

2. in row 3, after l and r meet each other, l is located at the first position equal to or greater than the vertex element, which is exchanged with the vertex element. Then, attention is placed at the position where it should be located.

 1 class Solution { 2     //param k : description of k 3     //param numbers : array of numbers 4     //return: description of return 5     public int kthLargestElement(int k, ArrayList<Integer> numbers) { 6         // write your code here 7         if (k > numbers.size()) return 0; 8         return helper(numbers.size()-k+1, numbers, 0, numbers.size()-1);  9     }10     11     public int helper(int k, ArrayList<Integer> numbers, int start, int end) {12         int l=start, r=end;13         int pivot = end;14         while (true) {15             while (numbers.get(l)<numbers.get(pivot) && l<r) {16                 l++;17             }18             while (numbers.get(r)>=numbers.get(pivot) && l<r) {19                 r--;20             }21             if (l == r) break;22             swap(numbers, l, r);23         }24         swap(numbers, l, end);  // l here is the first one which is bigger than pivot, swap it with the pivot25         if (l+1 == k) return numbers.get(l);26         else if (l+1 < k) return helper(k, numbers, l+1, end);27         else return helper(k, numbers, start, l-1);28     }29     30     public void swap(ArrayList<Integer> numbers, int l, int r) {31         int temp = numbers.get(l);32         numbers.set(l, numbers.get(r).intValue());33         numbers.set(r, temp);34     }35 };

 

Related Article

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.