Given an unsorted array of integers nums and an integer k, return the kth largest element in the array.
By kth largest element, we mean the kth largest element in the sorted order, not the kth distinct element.
Follow-up: Can you solve it without sorting?
Example 1:
Input: nums = [2,3,1,5,4], k = 2
Output: 4Example 2:
Input: nums = [2,3,1,1,5,5,4], k = 3
Output: 4Constraints:
1 <= k <= nums.length <= 10000-1000 <= nums[i] <= 1000
You should aim for a solution as good or better than O(nlogk) time and O(k) space, where n is the size of the input array, and k represents the rank of the largest number to be returned (i.e., the k-th largest element).
A naive solution would be to sort the array in descending order and return the k-th largest element. This would be an O(nlogn) solution. Can you think of a better way? Maybe you should think of a data structure which can maintain only the top k largest elements.
We can use a Min-Heap, which stores elements and keeps the smallest element at its top. When we add an element to the Min-Heap, it takes O(logk) time since we are storing k elements in it. Retrieving the top element (the smallest in the heap) takes O(1) time. How can this be useful for finding the k-th largest element?
The k-th largest element is the smallest element among the top k largest elements. This means we only need to maintain k elements in our Min-Heap to efficiently determine the k-th largest element. Whenever the size of the Min-Heap exceeds k, we remove the smallest element by popping from the heap. How do you implement this?
We initialize an empty Min-Heap. We iterate through the array and add elements to the heap. When the size of the heap exceeds k, we pop from the heap and continue. After the iteration, the top element of the heap is the k-th largest element.
Before attempting this problem, you should be comfortable with:
If you sort the entire array, all elements will be arranged from smallest to largest.
Once sorted:
n - k.So the problem becomes:
→ Sort the array and pick the element that is k steps from the end.
This is simple but not the most efficient, because sorting takes O(n log n).
index = n - k.Instead of sorting the whole array, we only need to keep track of the k largest elements seen so far.
A min-heap is perfect for this:
k, then:k largest elements seen so far.k) will be the k-th largest element.Process:
k, remove the smallest element.This avoids sorting the entire array and keeps memory small.
k, pop the smallest element.Where is the length of the array .
Quick Select is a selection algorithm that works like QuickSort but only explores the side of the array that contains the answer.
Key idea:
Because we eliminate half the array each time, this approach is much faster on average than full sorting.
For the k-th largest:
target = n - k.l and r:p.p == target, return the pivot value.p > target, repeat Quick Select on the left half.p < target, repeat on the right half.class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
k = len(nums) - k
def quickSelect(l, r):
pivot, p = nums[r], l
for i in range(l, r):
if nums[i] <= pivot:
nums[p], nums[i] = nums[i], nums[p]
p += 1
nums[p], nums[r] = nums[r], nums[p]
if p > k:
return quickSelect(l, p - 1)
elif p < k:
return quickSelect(p + 1, r)
else:
return nums[p]
return quickSelect(0, len(nums) - 1)Quick Select finds the k-th largest (or smallest) element without sorting the whole array.
This optimal version improves the classic Quick Select by:
The key idea is still the same:
This drastically reduces unnecessary work and leads to O(n) average time.
targetIndex = k - 1 (because array is arranged in descending order in this variant).left = 0 and right = n - 1.left, left+1, right, mid).j.j == targetIndex: return the pivot value.j > targetIndex: search only in the left part (right = j - 1).j < targetIndex: search only in the right part (left = j + 1).class Solution:
def partition(self, nums: List[int], left: int, right: int) -> int:
mid = (left + right) >> 1
nums[mid], nums[left + 1] = nums[left + 1], nums[mid]
if nums[left] < nums[right]:
nums[left], nums[right] = nums[right], nums[left]
if nums[left + 1] < nums[right]:
nums[left + 1], nums[right] = nums[right], nums[left + 1]
if nums[left] < nums[left + 1]:
nums[left], nums[left + 1] = nums[left + 1], nums[left]
pivot = nums[left + 1]
i = left + 1
j = right
while True:
while True:
i += 1
if not nums[i] > pivot:
break
while True:
j -= 1
if not nums[j] < pivot:
break
if i > j:
break
nums[i], nums[j] = nums[j], nums[i]
nums[left + 1], nums[j] = nums[j], nums[left + 1]
return j
def quickSelect(self, nums: List[int], k: int) -> int:
left = 0
right = len(nums) - 1
while True:
if right <= left + 1:
if right == left + 1 and nums[right] > nums[left]:
nums[left], nums[right] = nums[right], nums[left]
return nums[k]
j = self.partition(nums, left, right)
if j >= k:
right = j - 1
if j <= k:
left = j + 1
def findKthLargest(self, nums: List[int], k: int) -> int:
return self.quickSelect(nums, k - 1)The problem asks for the k-th largest element, not the k-th smallest. When using sorting, the k-th largest is at index n - k, not at index k - 1. Similarly, in QuickSelect, you must convert to the correct target index. Mixing up these indices is a very common source of off-by-one errors.
For the heap approach, a min-heap of size k is the correct choice because it keeps the smallest of the k largest elements at the top. Using a max-heap would require storing all n elements and extracting k times, which is less efficient. The min-heap approach maintains O(k) space and O(n log k) time.
QuickSelect degrades to O(n^2) time when the pivot choice is consistently poor, such as always picking the last element on an already sorted or reverse-sorted array. This can cause time limit exceeded errors. Using randomized pivot selection or median-of-three pivot selection helps avoid this worst case and keeps the average complexity at O(n).