20210411 LeetCode 每日一题(丑数 II)

152 阅读2分钟

题目描述

原题链接:丑数 II

给你一个整数 n ,请你找出并返回第 n 个 丑数 。

丑数 就是只包含质因数 2、3 和/或 5 的正整数。

示例

输入:n = 10
输出:12
解释:[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 个丑数组成的序列。
输入:n = 1
输出:1
解释:1 通常被视为丑数。

解答

这个题是丑数的扩展题,题目要求从判断一个数是否为丑数变成了求第 N 个丑数,但思路却完全不一样了。

从题目要求来看,有点类似求第 N 小的数,所以立马就能想到堆,只不过数列需要我们动态的求出来。利用小顶堆,每次弹出最小的数计算其 _ 2、_ 3、* 5 的值后放入堆中,当弹出的第 N 个数就是答案了,值得注意的是,计算出来的数有可能是重复的,所以需要维护一个集合,记录下进入过堆的数,如果某个数已经进入过堆了,就不在重复进入。

/**
 * @param {number} n
 * @return {number}
 */
var nthUglyNumber = function (n) {
  const factors = [2, 3, 5]
  const set = new Set()
  const heap = new MinHeap()
  heap.insert(1)
  for (let i = 0; i < n - 1; i++) {
    const num = heap.pop()
    for (let factor of factors) {
      const next = num * factor
      if (!set.has(next)) {
        heap.insert(next)
        set.add(next)
      }
    }
  }
  return heap.pop()
}

class MinHeap {
  constructor() {
    this.heap = []
  }

  insert(value) {
    this.heap.push(value)
    this.shiftUp(this.heap.length - 1)
  }

  pop() {
    this.heap[0] = this.heap.pop()
    this.shiftDown(0)
    return this.heap[0]
  }

  getParentIndex(index) {
    return (index - 1) >> 1
  }
  getLeftIndex(index) {
    return index * 2 + 1
  }
  getRightIndex(index) {
    return index * 2 + 2
  }
  shiftUp(index) {
    if (index === 0) return
    const parentIndex = this.getParentIndex(index)
    if (this.heap[parentIndex] > this.heap[index]) {
      this.swap(parentIndex, index)
      this.shiftUp(parentIndex)
    }
  }
  shiftDown(index) {
    const leftIndex = this.getLeftIndex(index)
    const 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)
    }
  }
  swap(i1, i2) {
    ;[this.heap[i1], this.heap[i2]] = [this.heap[i2], this.heap[i1]]
  }
}

复杂度分析

  • 时间复杂度:O(N logN),堆的每次插入和弹出都需要 logN 时间,且需要至多遍历 3N 个数即复杂度 O(N),所以综合平均时间复杂度为 O(N logN)
  • 空间复杂度:O(N),集合需要至多 3N 个空间,堆需要至多 N 个空间,综合空间复杂度为 O(N)