Merge pull request #493 from jackeyjia/patch-13

add js solution for findLengthOfLCIS
This commit is contained in:
程序员Carl
2021-07-16 08:50:21 +08:00
committed by GitHub

View File

@ -218,6 +218,49 @@ class Solution:
Go
Javascript
> 动态规划:
```javascript
const findLengthOfLCIS = (nums) => {
let dp = Array(nums.length).fill(1);
for(let i = 0; i < nums.length - 1; i++) {
if(nums[i+1] > nums[i]) {
dp[i+1] = dp[i]+ 1;
}
}
return Math.max(...dp);
};
```
> 贪心法:
```javascript
const findLengthOfLCIS = (nums) => {
if(nums.length === 1) {
return 1;
}
let maxLen = 1;
let curMax = 1;
let cur = nums[0];
for(let num of nums) {
if(num > cur) {
curMax += 1;
maxLen = Math.max(maxLen, curMax);
} else {
curMax = 1;
}
cur = num;
}
return maxLen;
};
```