From 52a486101d0525074a7b1f6a54a98b5940f9ab50 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 19 May 2022 00:43:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880674.=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E8=BF=9E=E7=BB=AD=E9=80=92=E5=A2=9E=E5=BA=8F=E5=88=97?= =?UTF-8?q?.md=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/0674.最长连续递增序列.md | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 56e95d97..6ec4a6c7 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -319,6 +319,45 @@ const findLengthOfLCIS = (nums) => { }; ``` +TypeScript: + +> 动态规划: + +```typescript +function findLengthOfLCIS(nums: number[]): number { + /** + dp[i]: 前i个元素,以nums[i]结尾,最长连续子序列的长度 + */ + const dp: number[] = new Array(nums.length).fill(1); + let resMax: number = 1; + for (let i = 1, length = nums.length; i < length; i++) { + if (nums[i] > nums[i - 1]) { + dp[i] = dp[i - 1] + 1; + } + resMax = Math.max(resMax, dp[i]); + } + return resMax; +}; +``` + +> 贪心: + +```typescript +function findLengthOfLCIS(nums: number[]): number { + let resMax: number = 1; + let count: number = 1; + for (let i = 0, length = nums.length; i < length - 1; i++) { + if (nums[i] < nums[i + 1]) { + count++; + } else { + count = 1; + } + resMax = Math.max(resMax, count); + } + return resMax; +}; +``` +