From 54d10fc3b893d50c278349c9de9d09bab5924ae7 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 19 May 2022 00:21:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880300.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E4=B8=8A=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0300.最长上升子序列.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index f68edb5a..5ccb2d76 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -220,6 +220,27 @@ const lengthOfLIS = (nums) => { }; ``` +TypeScript + +```typescript +function lengthOfLIS(nums: number[]): number { + /** + dp[i]: 前i个元素中,以nums[i]结尾,最长子序列的长度 + */ + const dp: number[] = new Array(nums.length).fill(1); + let resMax: number = 0; + for (let i = 0, length = nums.length; i < length; i++) { + for (let j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + resMax = Math.max(resMax, dp[i]); + } + return resMax; +}; +``` +