classSolution { public: intquickselect(vector<int> &nums, int l, int r, int k){ if (l == r) return nums[k]; int partition = nums[l], i = l - 1, j = r + 1; while (i < j) { do i++; while (nums[i] < partition); do j--; while (nums[j] > partition); if (i < j) swap(nums[i], nums[j]); } if (k <= j)returnquickselect(nums, l, j, k); elsereturnquickselect(nums, j + 1, r, k); }
intfindKthLargest(vector<int> &nums, int k){ int n = nums.size(); returnquickselect(nums, 0, n - 1, n - k); } };
classSolution { intquickselect(int[] nums, int l, int r, int k) { if (l == r) return nums[k]; intx= nums[l], i = l - 1, j = r + 1; while (i < j) { do i++; while (nums[i] < x); do j--; while (nums[j] > x); if (i < j){ inttmp= nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } if (k <= j) return quickselect(nums, l, j, k); elsereturn quickselect(nums, j + 1, r, k); } publicintfindKthLargest(int[] _nums, int k) { intn= _nums.length; return quickselect(_nums, 0, n - 1, n - k); } }
intquickselect(int *nums, int l, int r, int k) { if (l == r) return nums[k]; int partition = nums[l], i = l - 1, j = r + 1; while (i < j) { do i++; while (nums[i] < partition); do j--; while (nums[j] > partition); if (i < j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } if (k <= j)return quickselect(nums, l, j, k); elsereturn quickselect(nums, j + 1, r, k); }
intfindKthLargest(int *nums, int numsSize, int k) { return quickselect(nums, 0, numsSize - 1, numsSize - k); }
classSolution { public: voidmaxHeapify(vector<int>& a, int i, int heapSize){ int l = i * 2 + 1, r = i * 2 + 2, largest = i; if (l < heapSize && a[l] > a[largest]) { largest = l; } if (r < heapSize && a[r] > a[largest]) { largest = r; } if (largest != i) { swap(a[i], a[largest]); maxHeapify(a, largest, heapSize); } }
voidbuildMaxHeap(vector<int>& a, int heapSize){ for (int i = heapSize / 2; i >= 0; --i) { maxHeapify(a, i, heapSize); } }
intfindKthLargest(vector<int>& nums, int k){ int heapSize = nums.size(); buildMaxHeap(nums, heapSize); for (int i = nums.size() - 1; i >= nums.size() - k + 1; --i) { swap(nums[0], nums[i]); --heapSize; maxHeapify(nums, 0, heapSize); } return nums[0]; } };
classSolution { publicintfindKthLargest(int[] nums, int k) { intheapSize= nums.length; buildMaxHeap(nums, heapSize); for (inti= nums.length - 1; i >= nums.length - k + 1; --i) { swap(nums, 0, i); --heapSize; maxHeapify(nums, 0, heapSize); } return nums[0]; }
publicvoidbuildMaxHeap(int[] a, int heapSize) { for (inti= heapSize / 2; i >= 0; --i) { maxHeapify(a, i, heapSize); } }
publicvoidmaxHeapify(int[] a, int i, int heapSize) { intl= i * 2 + 1, r = i * 2 + 2, largest = i; if (l < heapSize && a[l] > a[largest]) { largest = l; } if (r < heapSize && a[r] > a[largest]) { largest = r; } if (largest != i) { swap(a, i, largest); maxHeapify(a, largest, heapSize); } }
publicvoidswap(int[] a, int i, int j) { inttemp= a[i]; a[i] = a[j]; a[j] = temp; } }
voidmaxHeapify(int* a, int i, int heapSize) { int l = i * 2 + 1, r = i * 2 + 2, largest = i; if (l < heapSize && a[l] > a[largest]) { largest = l; } if (r < heapSize && a[r] > a[largest]) { largest = r; } if (largest != i) { int t = a[i]; a[i] = a[largest], a[largest] = t; maxHeapify(a, largest, heapSize); } }
voidbuildMaxHeap(int* a, int heapSize) { for (int i = heapSize / 2; i >= 0; --i) { maxHeapify(a, i, heapSize); } }
intfindKthLargest(int* nums, int numsSize, int k) { int heapSize = numsSize; buildMaxHeap(nums, heapSize); for (int i = numsSize - 1; i >= numsSize - k + 1; --i) { int t = nums[0]; nums[0] = nums[i], nums[i] = t; --heapSize; maxHeapify(nums, 0, heapSize); } return nums[0]; }
funcfindKthLargest(nums []int, k int)int { heapSize := len(nums) buildMaxHeap(nums, heapSize) for i := len(nums) - 1; i >= len(nums) - k + 1; i-- { nums[0], nums[i] = nums[i], nums[0] heapSize-- maxHeapify(nums, 0, heapSize) } return nums[0] }
funcbuildMaxHeap(a []int, heapSize int) { for i := heapSize/2; i >= 0; i-- { maxHeapify(a, i, heapSize) } }
funcmaxHeapify(a []int, i, heapSize int) { l, r, largest := i * 2 + 1, i * 2 + 2, i if l < heapSize && a[l] > a[largest] { largest = l } if r < heapSize && a[r] > a[largest] { largest = r } if largest != i { a[i], a[largest] = a[largest], a[i] maxHeapify(a, largest, heapSize) } }