Maison > Article > interface Web > Kième plus grand élément d'un tableau
https://leetcode.com/problems/kth-largest-element-in-an-array/description
Si le tableau est [8, 6, 12, 9, 3, 4] et que k vaut 2, vous devez trouver le 2ème plus grand élément de ce tableau. Tout d'abord, vous trierez le tableau : [3, 4, 6, 8, 9, 12] La sortie sera 9 car il s'agit du deuxième plus grand élément.
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]; };
Donc, la complexité temporelle globale est 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; };
Élément actuel = 3
Élément actuel = 5
Élément actuel = 4
Remarque : même si Leetcode restreint la sélection rapide, vous devez vous souvenir de cette approche car elle réussit la plupart des cas de test
//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; }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!