https://leetcode.com/problems/kth-largest-element-in-an-array/description
If the array is [8, 6, 12, 9, 3, 4] and k is 2, you need to find the 2nd largest element in this array. First, you will sort the array: [3, 4, 6, 8, 9, 12] The output will be 9 because it is the second-largest element.
var findKthLargest = function(nums, k) { // Sort the array in ascending order nums.sort((a, b) => a - b); // Return the kth largest element return nums[nums.length - k]; };
So, the overall time complexity is O(n log n).
var findKthLargest = function(nums, k) { // Create a min-heap using a priority queue let minHeap = new MinPriorityQueue(); // Add the first k elements to the heap for (let i = 0; i < k; i++) { //minHeap.enqueue(nums[i]): Adds the element nums[i] to the min-heap. minHeap.enqueue(nums[i]); } // Iterate through the remaining elements for (let i = k; i < nums.length; i++) { //minHeap.front().element: Retrieves the smallest element in the min-heap without removing it. if (minHeap.front().element < nums[i]) { // minHeap.dequeue(): Removes the smallest element from the min-heap. minHeap.dequeue(); // Add the current element minHeap.enqueue(nums[i]); } } // The root of the heap is the kth largest element return minHeap.front().element; };
Current element = 3
Current element = 5
Current element = 4
Note: Even though Leetcode restricts quick select, you should remember this approach because it passes most test cases
//Quick Select Algo function quickSelect(list, left, right, k) if left = right return list[left] Select a pivotIndex between left and right pivotIndex := partition(list, left, right, pivotIndex) if k = pivotIndex return list[k] else if k < pivotIndex right := pivotIndex - 1 else left := pivotIndex + 1
var findKthLargest = function(nums, k) { // Call the quickSelect function to find the kth largest element return quickSelect(nums, 0, nums.length - 1, nums.length - k); }; function quickSelect(nums, low, high, index) { // If the low and high pointers are the same, return the element at low if (low === high) return nums[low]; // Partition the array and get the pivot index let pivotIndex = partition(nums, low, high); // If the pivot index is the target index, return the element at pivot index if (pivotIndex === index) { return nums[pivotIndex]; } else if (pivotIndex > index) { // If the pivot index is greater than the target index, search in the left partition return quickSelect(nums, low, pivotIndex - 1, index); } else { // If the pivot index is less than the target index, search in the right partition return quickSelect(nums, pivotIndex + 1, high, index); } } function partition(nums, low, high) { // Choose the pivot element let pivot = nums[high]; let pointer = low; // Rearrange the elements based on the pivot for (let i = low; i < high; i++) { if (nums[i] <= pivot) { [nums[i], nums[pointer]] = [nums[pointer], nums[i]]; pointer++; } } // Place the pivot element in its correct position [nums[pointer], nums[high]] = [nums[high], nums[pointer]]; return pointer; }
The above is the detailed content of Kth Largest Element in an Array. For more information, please follow other related articles on the PHP Chinese website!