[Leetcode] Heap, Divide and conquer--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.

Solution:

1st method using quicksort to sort in non-ascending order, and get the kth number. n = len(nums), time complexity is o(nlogn) -

2nd method. quickselect. set the pivot (e.g. the first element ) and rearrange all the element less than pivot to be in the left, bigger than pivot to be in the right; then compare with the pivot index with k to decide to go next in the left part or right part to recursively do the same thing reference introduction to algorithm texbook or --reference https://en.wikipedia.org/wiki/Selection_algorithm https://discuss.leetcode.com/topic/14611/java-quick-select/2 random algorithm ; Amortized time complexity is o(n), worst case is o(n^2)

 1  def select(nums, l, r, index):
 2             if r == l:
 3                 return nums[l]
 4             #pivot index
 5             pind = random.randint(l, r)
 6             nums[l], nums[pind] = nums[pind], nums[l]   #move pivot to the beginning of the
 7
 8             #partition around pivot
 9             i = l
10             for j in xrange(l+1, r+1):
11                 if nums[j] < nums[l]:
12                     i += 1
13                     nums[i], nums[j] = nums[j], nums[i]
14             nums[i], nums[l] = nums[l],nums[i]
15
16             if index == i:
17                 return nums[i]
18             elif index < i:
19                 return select(nums, l, i-1, index)
20             else:
21                 return select(nums, i+1, r, index)
22
23         index = len(nums) - k
24         if nums is None or len(nums) < 1:
25             return 0
26         return select(nums, 0, len(nums)-1, index)

3rd method, use heap if built-in heapq is allowed in python,

1 h = []
2         for n in nums:
3             heapq.heappush(h, -1*n)     #min-heap
4
5         for i in range(0, k-1):
6             heapq.heappop(h)
7
8         return -1*heapq.heappop(h)

4th - another method that I have not come up with, which is to use multiset, similar to heap ( it is interesting). --reference https://www.liuchuo.net/archives/3016.

时间: 2024-10-10 02:04:33

[Leetcode] Heap, Divide and conquer--215. Kth Largest Element in an Array的相关文章

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

刷题215. Kth Largest Element in an Array

一.题目说明 题目215. Kth Largest Element in an Array,在一个无序数组中找第k大的元素.难度是Medium! 二.我的解答 这个题目最直观的解答是,先对数组排序,然后直接返回: class Solution{ public: int findKthLargest(vector<int>& nums,int k){ sort(nums.begin(),nums.end()); return nums[nums.size()-k]; } }; 性能如下:

【LeetCode】215. Kth Largest Element in an Array (2 solutions)

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

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

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】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 ≤ arr

leetcode 215. Kth Largest Element in an Array 寻找第k个大的数---------- java

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

LeetCode OJ 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

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