diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 4f571d09..b1ed80c3 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -338,6 +338,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; +}; +``` +