LintCode Kth Largest Element

时间:2024-01-05 18:15:44

原题链接在这里:http://www.lintcode.com/en/problem/kth-largest-element/#

在LeetCode上也有一道,采用了标准的quickSelect 方法,另外写了一篇帖子,代码更加模块化。

采用的quickSelect方法,取出pivot, 比pivot 小的都放在左边,比pivot大的都放在右边,若是pivot左边包括pivot的个数恰巧等于k, 就返回pivot.

若是大于k, 就在左边递归寻找第k小的数,若是大于k,就在右边递归寻找 第 (k-left)小的数。

题目中说要找第k 大的数,其实就是找 numbers.size()-k+1小的数。

pivot取的是最后一个数,跳出loop时l所在位置一定比pivot大,需要换回来,调换l和end上的数。

为了保证跳出loop时l上的数比pivot大,中间的 while 循环条件是 numbers.get(l) < pivot 就移动l,另一个while loop 条件却是 number.get(r) >= pivot 移动r, 这是为了防止陷入infinite loop.

AC Java:

 class Solution {
//param k : description of k
//param numbers : array of numbers
//return: description of return
public int kthLargestElement(int k, ArrayList<Integer> numbers) {
return findK(numbers.size()-k,numbers,0,numbers.size()-1);
}
private int findK(int k, ArrayList<Integer> numbers, int start, int end){
if(start >= end){
return numbers.get(start);
}
int m = partition(numbers, start, end);
if(m == k){
return numbers.get(m);
}else if(m < k){
return findK(k, numbers, m+1, end);
}else{
return findK(k, numbers, start, m-1);
}
} private int partition(ArrayList<Integer> numbers, int start, int end){
int pivot = numbers.get(start);
int m = start;
int n = start + 1;
while(n<=end){
if(numbers.get(n) < pivot){
swap(numbers, ++m, n);
}
n++;
}
swap(numbers, start, m);
return m;
} private void swap(ArrayList<Integer> numbers, int l, int r){
int temp = numbers.get(l);
numbers.set(l,numbers.get(r));
numbers.set(r,temp);
}
};

另外一种写法:

 class Solution {
//param k : description of k
//param numbers : array of numbers
//return: description of return
public int kthLargestElement(int k, ArrayList<Integer> numbers) {
if(numbers == null || numbers.size() == 0 || k<1){
return 0;
}
return getKth(numbers.size()-k+1, numbers, 0, numbers.size()-1);
} private int getKth(int k, ArrayList<Integer> numbers, int start, int end){
int pivot = numbers.get(end);
int l = start;
int r = end;
while(true){
while(numbers.get(l) < pivot && l<r){
l++;
}
while(numbers.get(r) >= pivot && r>l){
r--;
}
if(l == r){
break;
}
swap(numbers, l, r);
}
//l element is larger than pivot, swap it with pivot
swap(numbers, l, end);
if(k == l+1){
return numbers.get(l);
}else if(k < l+1){
return getKth(k, numbers, start, l-1);
}else{
return getKth(k, numbers, l+1, end);
}
} private void swap(ArrayList<Integer> numbers, int l, int r){
int temp = numbers.get(l);
numbers.set(l,numbers.get(r));
numbers.set(r,temp);
}
};