LintCode Kth Largest Element

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

采用的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:

 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         if(numbers == null || numbers.size() == 0 || k<1){
 7             return 0;
 8         }
 9         return getKth(numbers.size()-k+1, numbers, 0, numbers.size()-1);
10     }
11
12     private int getKth(int k, ArrayList<Integer> numbers, int start, int end){
13         int pivot = numbers.get(end);
14         int l = start;
15         int r = end;
16         while(true){
17             while(numbers.get(l) < pivot && l<r){
18                 l++;
19             }
20             while(numbers.get(r) >= pivot && r>l){
21                 r--;
22             }
23             if(l == r){
24                 break;
25             }
26             swap(numbers, l, r);
27         }
28         //l element is larger than pivot, swap it with pivot
29         swap(numbers, l, end);
30         if(k == l+1){
31             return numbers.get(l);
32         }else if(k < l+1){
33             return getKth(k, numbers, start, l-1);
34         }else{
35             return getKth(k, numbers, l+1, end);
36         }
37     }
38
39     private void swap(ArrayList<Integer> numbers, int l, int r){
40         int temp = numbers.get(l);
41         numbers.set(l,numbers.get(r));
42         numbers.set(r,temp);
43     }
44 };
时间: 2024-12-17 19:39:20

LintCode Kth Largest Element的相关文章

Lintcode: Kth Largest Element 解题报告

Kth Largest Element 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 3th largest element is 4 Challenge O(n) time, O(1) space 原题链接: http://www.lintcode.com/en/problem/kth-largest-element

Kth Largest Element II Lintcode

Find K-th largest element in an array. and N is much larger than k. Notice 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, 2ndlargest element is 4, 3rd

Lintcode5 Kth Largest Element solution 题解

[题目描述] Find K-th largest element in an array. Notice:You can swap elements in the array 在数组中找到第k大的元素 注意:你可以交换数组中的元素的位置 [题目链接] http://www.lintcode.com/en/problem/kth-largest-element/ [题目解析] sort的方法:一开始看到这道题肯定觉得很简单,只要sort一下,然后return特定index的value就可以了,但是

215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note:You may assume k is always valid, 1 ≤ k ≤ array's le

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct eleme

LeetCode 215 : Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's

Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

LeetCode215:Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array'

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. 解题思路: 本题是<算法导论>原题,