LeetCode 0215. Kth Largest Element in an Array Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
0215. Kth Largest Element in an Array

Description

Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

Can you solve it without sorting?

 

Example 1:

Input: nums = [3,2,1,5,6,4], k = 2
Output: 5

Example 2:

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4

 

Constraints:

  • 1 <= k <= nums.length <= 105
  • -104 <= nums[i] <= 104

Solutions

Solution 1: Quick Select

Quick Select is an algorithm for finding the k^{th} largest or smallest element in an unsorted array. Its basic idea is to select a pivot element each time, dividing the array into two parts: one part contains elements smaller than the pivot, and the other part contains elements larger than the pivot. Then, based on the position of the pivot, it decides whether to continue the search on the left or right side until the k^{th} largest element is found.

The time complexity is O(n), and the space complexity is O(log n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScriptRust
class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: def quick_sort(l: int, r: int) -> int: if l == r: return nums[l] i, j = l - 1, r + 1 x = nums[(l + r) >> 1] while i < j: while 1: i += 1 if nums[i] >= x: break while 1: j -= 1 if nums[j] <= x: break if i < j: nums[i], nums[j] = nums[j], nums[i] if j < k: return quick_sort(j + 1, r) return quick_sort(l, j) n = len(nums) k = n - k return quick_sort(0, n - 1)(code-box)

Solution 2: Priority Queue (Min Heap)

We can maintain a min heap minQ of size k, and then iterate through the array nums, adding each element to the min heap. When the size of the min heap exceeds k, we pop the top element of the heap. This way, the final k elements in the min heap are the k largest elements in the array, and the top element of the heap is the k^{th} largest element.

The time complexity is O(nlog k), and the space complexity is O(k). Here, n is the length of the array nums.

PythonJavaC++GoTypeScriptRust
class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: return nlargest(k, nums)[-1](code-box)

Solution 3: Counting Sort

We can use the idea of counting sort, counting the occurrence of each element in the array nums and recording it in a hash table cnt. Then, we iterate over the elements i from largest to smallest, subtracting the occurrence count cnt[i] each time, until k is less than or equal to 0. At this point, the element i is the k^{th} largest element in the array.

The time complexity is O(n + m), and the space complexity is O(n). Here, n is the length of the array nums, and m is the maximum value among the elements in nums.

PythonJavaC++GoTypeScriptRust
class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: cnt = Counter(nums) for i in count(max(cnt), -1): k -= cnt[i] if k <= 0: return i(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !