【算法】JS 滑动窗口&(最大/小堆 | 单调队列) 力扣周赛6911. 不间断子数组

题目链接

6911. 不间断子数组

题目描述

题目思路

滑动窗口,只要左右指针区间内的数据满足不间断子数组条件,右指针不断向右移动,倘若不满足,则需要剔除掉左指针指向的那个数字

假设[2,1,2,3] left 0 right 3, 此时要在滑动窗口中去掉指向left的数字2,与2相关的不间断子数组有3个[[2],[2,1],[2,1,2]],答案+3,即right-left的值,然后左指针不断右移,直到区间内的数字重新满足不间断子数组条件。

大体思路肯定是滑动窗口,问题就在于如何维护这个窗口,快速找到当前窗口内的最大值和最小值的差值,我一开始的思路是二分,把这个窗口内的数字用一个新的有序数组维护,但是超时了,下面给出另外两种解决方法

最大/小堆

/**
 * @param {number[]} nums
 * @return {number}
 */
var continuousSubarrays = function(nums) {
    let left = 0, right = 0, ans = 0
    const len = nums.length
    const bigH = new Heap(), smallH = new Heap((a, b) => a < b)

    while (right < len) {
        bigH.push(nums[right])
        smallH.push(nums[right])

        while (left < right && bigH.peek() - smallH.peek() > 2) {
            bigH.remove(nums[left])
            smallH.remove(nums[left])    
            ans += right - left // 假设[2,1,2,3] left 0 right 3, 此时要在滑动窗口中去掉数字2,与2相关的不间断子数组有3个[[2],[2,1],[2,1,2]]
            left++
        }
        right++
    }
    ans += (1 + right - left) * (right - left) / 2 // (首项 + 尾项) * 项数 / 2
    return ans
};
class Heap {
  constructor(compare) {
    this.heap = [null]
    this.compare = compare ?? ((a, b) => a > b) // 默认大顶堆
  }
  get size() { // O(1)
    return this.heap.length - 1
  }
  push(v) { // O(1)
    this.heap.push(v)
    this.up(this.size)
  }
  up(i) { // O(log n)
    while (i > 1 && this.compare(this.heap[i], this.heap[this.parent(i)])) {
      this.swap(this.parent(i), i)
      i = this.parent(i)
    }
  }
  down(i) { // O(log n)
    let temp = i
    if (this.left(i) <= this.size && this.compare(this.heap[this.left(i)], this.heap[temp])) temp = this.left(i)
    if (this.right(i) <= this.size && this.compare(this.heap[this.right(i)], this.heap[temp])) temp = this.right(i)
    if (temp !== i) {
      this.swap(temp, i)
      this.down(temp)
    }
  }
  pop() { // O(log n)
    if (this.size === 0) return null
    this.swap(1, this.size)
    const head = this.heap.pop()
    this.down(1)
    return head
  }
  remove(num) { // O(n)
    const index = this.heap.indexOf(num);
    if (index === -1) {
      return;
    }

    // 将要删除的数字与堆尾部的元素交换位置
    this.swap(index, this.size);
    this.heap.pop(); // 移除堆尾部的元素

    // 根据交换后的位置,判断是否需要进行上浮或下沉操作,以保持堆的性质

    if (index === this.size + 1) {
      // 如果删除的是堆尾部的元素,不需要进行堆化操作
      return;
    }
    const parentIndex = this.parent(index);
    if (index > 1 && this.compare(this.heap[index], this.heap[parentIndex])) {
      this.up(index);
    } else {
      this.down(index);
    }
  }
  peek() { // O(1)
    return this.size === 0 ? null : this.heap[1]
  }
  left(i) { // O(1)
    return i * 2
  }
  right(i) { // O(1)
    return i * 2 + 1
  }
  parent(i) { // O(1)
    return i >> 1
  }
  swap(i, j) { // O(1)
    [this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]
  }
}

单调队列

/**
 * @param {number[]} nums
 * @return {number}
 */
var continuousSubarrays = function(nums) {
  let i = 0, j = 0, ans = 0
  let minQ = [], maxQ = [] 
  while (j < nums.length) {
    // 如果不满足单调递增,需要舍弃(后面的元素更大,前面较小的元素就没必要维护)
    while (minQ.length && minQ[minQ.length - 1] > nums[j]) {
      minQ.pop()
    }
    // 如果不满足单调递减,需要舍弃(后面的元素更小,前面较小的元素就没必要维护)
    while (maxQ.length && maxQ[maxQ.length - 1] < nums[j]) {
      maxQ.pop()
    }

    minQ.push(nums[j])
    maxQ.push(nums[j])

    // 缩小左端点
    while (minQ.length && maxQ.length && maxQ[0] - minQ[0] > 2) {
      if (minQ[0] == nums[i]) {
        minQ.shift()
      }
      if (maxQ[0] == nums[i]) {
        maxQ.shift()
      }
      i++
    }
    ans += j - i + 1
    j++
  }
  return ans
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值