Skip to content

300. 最长递增子序列 ​

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

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

dp[i] 为以 i 结尾的最长递增子序列长度

typescript
/**
 * @difficulty medium
 * @tags dp,数组,子序列
 * @time O(n^2)
 * @space O(n)
 * @note dp[i] 为以 i 结尾的最长递增子序列长度
 * @300. 最长递增子序列
 */
// dp dp[i]表示以i结尾的最长递增子序列的长度
let nums = [10, 9, 2, 5, 3, 7, 101, 18]
let res = 0
let dp = new Array(nums.length).fill(1)
for (let i = 1; i < nums.length; i++) {
    for (let j = 0; j < i; j++) {
        if (nums[j] < nums[i]) {
            dp[i] = Math.max(dp[i], dp[j] + 1)
        }
    }
    res = Math.max(res, dp[i])
}
console.log(dp)
console.log(res)

源码:ts/leetcode/300. 最长递增子序列.ts


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