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/

SOLUTION 1:

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

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

时间复杂度:如果我们是这样的数组:1,2,3,4,5,然后又每一次取左边的当pivot,就会达到最坏的时间复杂度。也就是O(N2)

我们也有一些解决方法:

以下来自维基,我们可以取3个数进行平均。

http://stackoverflow.com/questions/7559608/median-of-three-values-strategy

The easiest solution is to choose a random pivot, which yields almost certain linear time. Deterministically, one can use median-of-3 pivot strategy (as in the quicksort), which yields linear performance on partially sorted data, as is common in the real world. However, contrived sequences can still cause worst-case complexity; David Musser describes a "median-of-3 killer" sequence that allows an attack against that strategy, which was one motivation for his introselect algorithm.

 1 package Algorithms.algorithm.NChapter.findKthNumber;
 2
 3 import java.util.ArrayList;
 4
 5 // The link:
 6
 7 // http://www.lintcode.com/en/problem/kth-largest-element/
 8
 9 class KthLargestElement_lintCode {
10     //param k : description of k
11     //param numbers : array of numbers
12     //return: description of return
13     public int kthLargestElement(int k, ArrayList<Integer> numbers) {
14         // write your code here
15         if (k < 1 || numbers == null) {
16             return 0;
17         }
18
19         return getKth(numbers.size() - k + 1, numbers, 0, numbers.size() - 1);
20     }
21
22     public int getKth(int k, ArrayList<Integer> numbers, int start, int end) {
23         // Choose the last one as the pivot
24         int pivot = numbers.get(end);
25
26         int left = start;
27         int right = end;
28
29         while (true) {
30             while (numbers.get(left) < pivot && left < right) {
31                 left++;
32             }
33
34             while (numbers.get(right) >= pivot && right > left) {
35                 right--;
36             }
37
38             if (left == right) {
39                 break;
40             }
41
42             swap(numbers, left, right);
43         }
44
45         // left: the first one which is bigger than pivot.
46         swap(numbers, left, end);
47
48         if (k == left + 1) {
49             return pivot;
50         // Try to find the element from the left side.
51         } else if (k < left + 1) {
52             return getKth(k, numbers, start, left - 1);
53         } else {
54         // Try to find the element from the right side.
55             return getKth(k, numbers, left + 1, end);
56         }
57     }
58
59     /*
60         Swap the two nodes.
61     */
62     public void swap(ArrayList<Integer> numbers, int n1, int n2) {
63         int tmp = numbers.get(n1);
64         numbers.set(n1, numbers.get(n2));
65         numbers.set(n2, tmp);
66     }
67 };

SOLUTION 2:

以下这些链接有详细的讨论,就不再一一叙述了。目前来讲Solution 1应该是最优的啦。

http://www.geeksforgeeks.org/k-largestor-smallest-elements-in-an-array/

http://www.quora.com/What-is-the-most-efficient-algorithm-to-find-the-kth-smallest-element-in-an-array-having-n-elements

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/algorithm/NChapter/findKthNumber/KthLargestElement_lintCode.java

时间: 2024-10-09 07:18:58

Lintcode: Kth Largest Element 解题报告的相关文章

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小的数. p

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

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. 解题思路: 本题是<算法导论>原题,

Leetcode题解(3):L215/Kth Largest Element in an Array

L215: 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 as

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