当前位置:网站首页>674. longest continuous increasing sequence force buckle JS

674. longest continuous increasing sequence force buckle JS

2022-07-01 03:52:00 Big drumsticks are best

Given an unordered array of integers , Find the longest and Successive increasing subsequences , And return the length of the sequence .

Successive increasing subsequences It can be made up of two subscripts l and r(l < r) determine , If for each l <= i < r, There are nums[i] < nums[i + 1] , So the subsequence [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] It's a continuous increasing subsequence .

Example 1:

Input :nums = [1,3,5,4,7]
Output :3
explain : The longest continuous increasing sequence is [1,3,5], The length is 3.
Even though [1,3,5,7] It's also a subsequence of ascending order , But it's not continuous , because 5 and 7 In the original array is 4 separate . 
Example 2:

Input :nums = [2,2,2,2,2]
Output :1
explain : The longest continuous increasing sequence is [2], The length is 1.

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

###  Their thinking

Here is the solution

Greedy Algorithm

a by b Maximum value of previous value

b Every time I meet the next one smaller than this, I will start a new wave

###  Code

```javascript

/**

 * @param {number[]} nums

 * @return {number}

 */

var findLengthOfLCIS = function(nums) {

  let a=1,b=1

  let len=nums.length

  for(let i=0;i<len-1;i++){

    if(nums[i]<nums[i+1]){

        b+=1

        a=Math.max(a,b)

        

    }

    else{

        b=1

    }

  }

  return a

};

```

原网站

版权声明
本文为[Big drumsticks are best]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160322080639.html