剑指OFFER----40-2最大的K个数(最小的K个数的变种)(js实现)

题目

  • 输入n个整数,找出其中最大的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最大的4个数字是8,7,6,5。
  • 面试通常会这样问:从1000000个数里面取前n大的数,或者从1000000个数里面取第n大的数

思路

  • 维护小顶堆

  • 求最小的K个数就建大顶堆,求最大的K个数就建小顶堆,然后之后遍历的元素不断和堆顶比较,重新调整堆


/**
 * @param {number[]} arr
 * @param {number} k
 * @return {number[]}
 */
 var getBigestNumbers = function (arr, k) {
  if (arr.length <= k) return arr
  const heap = new SmallHeap()
  for (let i = 0; i < arr.length; i++) {
    if (i <= k - 1) {
      heap.insert(arr[i])
      continue
    }
    heap.update(arr[i])
  }
  return heap.get()
}

function SmallHeap() {
  this.heap = [null]
}

SmallHeap.prototype.update = function(x) {
  if (x > this.heap[1]) {
    this.heap[1] = x
    this.heapify()
  }
}

SmallHeap.prototype.insert = function(x) {
  this.heap.push(x)
  let index = this.heap.length - 1
  while (Math.floor(index / 2) > 0 && this.heap[index] < this.heap[Math.floor(index / 2)]) {
    swap(this.heap, Math.floor(index / 2), index)
    index = Math.floor(index / 2)
  }
}

SmallHeap.prototype.heapify = function() {
  let i = 1
  while (i < this.heap.length) {
    let minIndex = i
    if (2 * i < this.heap.length && this.heap[2 * i] < this.heap[minIndex]) {
      minIndex = 2 * i
    }
    if (2 * i + 1 < this.heap.length && this.heap[2 * i + 1] < this.heap[minIndex]) {
      minIndex = 2 * i + 1
    }
    if (minIndex === i) break
    swap(this.heap, minIndex, i)
    i = minIndex
  }
}

SmallHeap.prototype.get = function() {
  return this.heap.slice(1)
}

function swap(arr, i, j) {
  const temp = arr[i]
  arr[i] = arr[j]
  arr[j] = temp
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值