- 38.2%

https://leetcode.com/problems/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 element.

1 | For example, |

Note:

You may assume k is always valid, 1 ≤ k ≤ array’s length.

总结：

方法一 :

先排序，后选出第k大的数字。面试肯定没啥用。

注意，对nums排序是，sort(nums.begin(), nums.end()), 不是sort(nums)

1 | class Solution { |

我的代码实现:

1 | class Solution { |

方法二:

求最大k值，建最小堆，效率O(nlogk)，第一次建堆k，每次排除一个头部，并重新维护堆性质logk。

学习如何建堆，默认建最大堆，如何建立最小堆。同时make_heap(res.begin(), res.end())，而不是make_heap(res)， 类似于sort。

学习如何设置cmp函数。

1 | class Solution { |

我的实现

需要先建最小堆， 然后从k至最后遍历

如果大于最小值，替换了，然后维护最小堆的性质

遍历到最后，堆里面是最大的k个，堆的root是第k大的值。

返回res[0]就好。

1 | class Solution { |

我的代码实现:

1 | class Solution { |

方法三:

使用快排里的patition方法，直到分配到一遍的是k个，并且对此进行排序。

1 | class Solution { |

我的代码实现：

1 | class Solution { |

我的代码实现：

1 | class Solution { |

资料，如何设置最小堆的比较函数

http://stackoverflow.com/questions/14016921/comparator-for-min-heap-in-c

1 | #include <vector> |

#### cpp

4 C++ Solutions using Partition, Max-Heap, priority_queue and multiset respectively

Well, this problem has a naive solution, which is to sort the array in descending order and return the k-1-th element.

1 | class Solution { |

However, sorting algorithm gives O(nlogn) complexity. Suppose n = 10000 and k = 2, then we are doing a lot of unnecessary operations. In fact, this problem has at least two simple and faster solutions.

Well, the faster solution has no mystery. It is also closely related to sorting. I will give two algorithms for this problem below, one using quicksort(specifically, the partition subroutine) and the other using heapsort.

Quicksort

In quicksort, in each iteration, we need to select a pivot and then partition the array into three parts:

- Elements smaller than the pivot;
- Elements equal to the pivot;
- Elements larger than the pivot.

Now, let’s do an example with the array [3, 2, 1, 5, 4, 6] in the problem statement. Let’s assume in each time we select the leftmost element to be the pivot, in this case, 3. We then use it to partition the array into the above 3 parts, which results in [1, 2, 3, 5, 4, 6]. Now 3 is in the third position and we know that it is the third smallest element. Now, do you recognize that this subroutine can be used to solve this problem?

In fact, the above partition puts elements smaller than the pivot before the pivot and thus the pivot will then be the k-th smallest element if it is at the k-1-th position. Since the problem requires us to find the k-th largest element, we can simply modify the partition to put elements larger than the pivot before the pivot. That is, after partition, the array becomes [5, 6, 4, 3, 1, 2]. Now we know that 3 is the 4-th largest element. If we are asked to find the 2-th largest element, then we know it is left to 3. If we are asked to find the 5-th largest element, then we know it is right to 3. So, in the average sense, the problem is reduced to approximately half of its original size, giving the recursion T(n) = T(n/2) + O(n) in which O(n) is the time for partition. This recursion, once solved, gives T(n) = O(n) and thus we have a linear time solution. Note that since we only need to consider one half of the array, the time complexity is O(n). If we need to consider both the two halves of the array, like quicksort, then the recursion will be T(n) = 2T(n/2) + O(n) and the complexity will be O(nlogn).

Of course, O(n) is the average time complexity. In the worst case, the recursion may become T(n) = T(n - 1) + O(n) and the complexity will be O(n^2).

Now let’s briefly write down the algorithm before writing our codes.

- Initialize left to be 0 and right to be nums.size() - 1;
- Partition the array, if the pivot is at the k-1-th position, return it (we are done);
- If the pivot is right to the k-1-th position, update right to be the left neighbor of the pivot;
- Else update left to be the right neighbor of the pivot.
- Repeat 2.

Now let’s turn it into code.

1 | class Solution { |

Heapsort

Well, this problem still has a tag “heap”. If you are familiar with heapsort, you can solve this problem using the following idea:

- Build a max-heap for nums, set heap_size to be nums.size();
- Swap nums[0] (after buding the max-heap, it will be the largest element) with nums[heap_size - 1] (currently the last element). Then decrease heap_size by 1 and max-heapify nums (recovering its max-heap property) at index 0;
- Repeat 2 for k times and the k-th largest element will be stored finally at nums[heap_size].

Now I paste my code below. If you find it tricky, I suggest you to read the Heapsort chapter of Introduction to Algorithms, which has a nice explanation of the algorithm. My code simply translates the pseudo code in that book :-)

1 | class Solution { |

If we are allowed to use the built-in priority_queue, the code will be much more shorter :-)

1 | class Solution { |

Well, the priority_queue can also be replaced by multiset :-)

1 | class Solution { |

4ms c++ solution. straightforward to find largest k. kind like a partition version.

1 | class Solution { |

Python different solutions with comments (bubble sort, selection sort, heap sort and quick sort).

1 | # O(nlgn) time |

https://discuss.leetcode.com/topic/20740/share-my-python-solution-with-quickselect-idea

Share my Python solution with QuickSelect idea

1 | class Solution: |

#### cpp

Solution 1:

20ms, 53,47%, June.18th, 2016

https://leetcode.com/discuss/38336/solutions-partition-priority_queue-multiset-respectively

1 | class Solution { |

#### python

Solution Mine:

52ms, 88.30%, June.18th, 2016

1 | class Solution(object): |

Solution 1:

3164ms, 7.6%, June.18th, 2016

https://leetcode.com/discuss/50389/share-my-python-solution-with-quickselect-idea

1 | class Solution(object): |

Solution 2:

60ms, 75.44%, June.18th, 2016

https://leetcode.com/discuss/53530/python-different-solutions-comments-bubble-selection-quick

1 | class Solution(object): |

6ms, 72.82%, June.18th, 2016

1 | https://leetcode.com/discuss/36966/solution-explained |

#### java

https://discuss.leetcode.com/topic/14597/solution-explained

Solution explained

This problem is well known and quite often can be found in various text books.

You can take a couple of approaches to actually solve it:

- O(N lg N) running time + O(1) memory

The simplest approach is to sort the entire input array and then access the element by it’s index (which is O(1)) operation:

1 | public int findKthLargest(int[] nums, int k) { |

- O(N lg K) running time + O(K) memory

Other possibility is to use a min oriented priority queue that will store the K-th largest values. The algorithm iterates over the whole input and maintains the size of priority queue.

1 | public int findKthLargest(int[] nums, int k) { |

- O(N) best case / O(N^2) worst case running time + O(1) memory

The smart approach for this problem is to use the selection algorithm (based on the partion method - the same one as used in quicksort).

1 | public int findKthLargest(int[] nums, int k) { |

O(N) guaranteed running time + O(1) space

So how can we improve the above solution and make it O(N) guaranteed? The answer is quite simple, we can randomize the input, so that even when the worst case input would be provided the algorithm wouldn’t be affected. So all what it is needed to be done is to shuffle the input.

1 | public int findKthLargest(int[] nums, int k) { |

There is also worth mentioning the Blum-Floyd-Pratt-Rivest-Tarjan algorithm that has a guaranteed O(N) running time.