Skip to content

406. 根据身高重建队列 ​

  • 题号:406
  • 来源:LeetCode
  • 难度:中等
  • 标签:贪心 排序 数组
  • 语言:TypeScript
  • 解法:1 个
  • 作者:lmliheng
  • 最近更新:2026-09-27

TypeScript · O(n^2) 时间 · O(n) 空间 · 更新于 2026-09-27

身高降序、k 升序排序后逐个插到第 k 位

typescript
/**
 * @difficulty medium
 * @tags 贪心,排序,数组
 * @time O(n^2)
 * @space O(n)
 * @note 身高降序、k 升序排序后逐个插到第 k 位
 * @406. 根据身高重建队列
 */
/**
 * @脑筋急转弯
 * @难题
 *
 *
 */

let people = [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]]
let res: number[][] = []
people.sort((a, b) => {
    if (a[0] === b[0]) {
        return a[1] - b[1]
    } else {
        return b[0] - a[0]
    }
})
console.log(people)
for (let i = 0; i < people.length; i++) {
    res.splice(people[i][1], 0, people[i]) // 插入
}

console.log(res)

源码:ts/leetcode/406. 根据身高重建队列.ts


在 GitHub 上查看题目所在目录:lmliheng/algorithm