LeetCode:215.数组中的第K个最大元素
解题思路看到“第K个最大元素”。考虑选择使用最小堆。
解题步骤构建一个最小堆,并依次把数组的值插入堆中。当堆的容量超过K,就删除堆顶。插入结束后,堆顶就是第K个最大元素。
leetcode在线 运行测试 可能是用本地环境跑分 ...有缓存 卡 大数有eroor
// 大数不通过 需要减少swap
class MinHeap{
constructor(){
this.heap = [];
}
getParentIndex(index){
return (index-1)>>1; //等同于Math.floor((index-1)/2)
}
getLeftIndex(index){
return 2*index+1;
}
getRightIndex(index){
return 2*index+2;
}
swap(i1,i2){
let temp = this.heap[i1];
this.heap[i1] = this.heap[i2];
this.heap[i2] = temp;
}
shiftUp(index){
if(index==0){return}
let parentIndex=this.getParentIndex(index); //获取父节点的索引
if(this.heap[parentIndex]>this.heap[index]){
this.swap(parentIndex,index); //交换父节点和当前节点的值
this.shiftUp(parentIndex)//继续向上调整,直到满足堆的性质为止
}
}
shiftDown(index){
let leftIndex=this.getLeftIndex(index); //获取左子节点的索引
let rightIndex=this.getRightIndex(index); //获取右子节点的索引
if(this.heap[leftIndex]<this.heap[index]){
this.swap(leftIndex,index);
this.shiftDown(leftIndex) //继续向上调整,直到满足堆的性质为止
}
if(this.heap[rightIndex]<this.heap[index]){
this.swap(rightIndex,index);
this.shiftDown(rightIndex) //继续向上调整,直到满足堆的性质为止
}
}
insert(val){
this.heap.push(val);
this.shiftUp(this.heap.length - 1)
}
pop(){
this.heap[0] = this.heap.pop();
this.shiftDown(0);
}
peek(){
return this.heap[0];
}
isEmpty(){
return this.heap.length === 0;
}
size(){
return this.heap.length;
}
getHeap(){
return this.heap;
}
remove(val){
for(let i=0;i<this.heap.length;i++){
if(this.heap[i]===val) return this.heap.splice(i,1);
}
}
max(k,arr){
arr.forEach(n => {
this.insert(n);
if(this.size()>k){
this.pop();
}
});
return this.peek();
}
allInsert(arr){
for(let i=0;i<arr.length;i++){
this.insert(arr[i]);
}
}
}
var findKthLargest = function (nums, k) {
let minHeap = new MinHeap()
nums.forEach(n => {
minHeap.insert(n);
if(minHeap.size()>k){
minHeap.pop();
}
});
return minHeap.peek();
};
//最快
var findKthLargest = function (nums, k) {
nums = nums.sort((a, b) => b - a)
return nums[k - 1]
};
var findKthLargest = function (nums, k) {
for (let i = 0; i < k; i++) {
for (let j = 0; j < nums.length - 1 - i; j++) {
if (nums[j] > nums[j + 1])
[nums[j], nums[j + 1]] = [nums[j + 1], nums[j]]
}
}
return nums[nums.length - k]
};
function binarySearch(arr, val) {
let low = 0
let high = arr.length - 1
while (low <= high) {
// mid 表示中间值
let mid = Math.floor(low + (high - low) / 2)
if (arr[mid] === val) return mid
val < arr[mid] ? high = mid - 1 : low = mid + 1
}
return null
}
let arr = [0, 1, 2, 3, 4, 5]
console.log(binarySearch(arr, 0));
function quickSort(arr) {
return quick(arr, 0, arr.length - 1)
}
function quick(arr, left, right) {
if (arr.length > 1) {
let index = partition(arr, left, right)
if (left < index - 1) quick(arr, left, index - 1)
if (index < right) quick(arr, index, right)
}
return arr
}
var findKthLargest = function (nums, k) {
let low = 0
let high = nums.length - 1
while (low <= high) {
const mid = partition(nums, low, high)
if (mid === k - 1) return nums[mid]
mid < k - 1 ? low = mid + 1 : high = mid - 1
}
}
function partition(arr, low, high) {
let mid = Math.floor(low + (high - low) / 2)
const pivot = arr[mid]; // 这里记得添加分号
// 把pivot放在arr的最后面
[arr[mid], arr[high]] = [arr[high], arr[mid]]
let i = low
// 把pivot排除在外,不对pivot进行排序
let j = high - 1
while (i <= j) {
while (arr[i] > pivot) i++
while (arr[j] < pivot) j--
if (i <= j) {
[arr[i], arr[j]] = [arr[j], arr[i]]
i++; j--;
}
}
// 因为arr[i]是属于left的,pivot也是属于left的
// 故我们可以把原本保护起来的pivot和现在数组的中间值交换
[arr[high], arr[i]] = [arr[i], arr[high]]
return i
}
//https://juejin.cn/post/6844903913150218248
'
标签:index,arr,215,return,heap,nums,let,数组,LeetCode From: https://www.cnblogs.com/KooTeam/p/18669537