码迷,mamicode.com
首页 > 其他好文 > 详细

Lintcode: Kth largest Element

时间:2015-03-09 07:01:05      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

Find K-th largest element in an array.

Note
You can swap elements in the array

Example
In array [9,3,2,4,8], the 3rd largest element is 4

In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.

Challenge
O(n) time, O(1) space

使用改进的Quicksort partition,可以达到O(n)的时间复杂度,并且不需要额外空间。

请参考: http://en.wikipedia.org/wiki/Quickselect#Time_complexity

技术分享

 Quickselect uses the same overall approach as quicksort, choosing one element as a pivot and partitioning the data in two based on the pivot, accordingly as less than or greater than the pivot. However, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side – the side with the element it is searching for. This reduces the average complexity from O(n log n) (in quicksort) to O(n) (in quickselect).

下面这个code几个注意的地方:

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

2. 第24行,l、r在相遇之后,l 所处的位置就是第一个大于等于pivot元素所在位置,把它跟pivot交换,pivot就放在了它应该在的位置

 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 pivot
25         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 };

 

Lintcode: Kth largest Element

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/4322657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!