674.最长连续递升序列(javascript)674.LongestContinuousIncreasingSubsequence
原创给定一个未排序的整数数组,找出最长的和 连续增加的子序列,并返回序列的长度。
不断增加的子序列 可以由两个下标组成 l 和 r(l < r)确定如果 l <= i < r,都有 nums[i] < nums[i + 1] ,然后是子序列 [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] 是一个不断增加的子序列。
Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].
示例 1:
输入:nums = [1,3,5,4,7]
输出:3
说明:最长的连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 它也是升序的子序列。, 但它不是连续的,因为 5 和 7 在原始号码组中 4 隔开。
示例 2:
输入:nums = [2,2,2,2,2]
输出:1
说明:最长的连续递增序列是 [2], 长度为1。
解决问题的思路:
- 设置一个max 变量用于储存最大的不断增加的子序列长度
- 遍历数组
- 设置另一个start 指向增量开始的指针,当发现该值小于前一个值时,前一个增量序列结束,start指针指向当前下标。
- 获取最大值,max和【start ,i】的长度
-
返回最大值
/**
- @param {number[]} nums
- @return {number} */ var findLengthOfLCIS = function (nums) { let start = 0 let max = 0 for (let i = 0; i < nums.length; i++) { if (i > 0 && nums[i] <= nums[i - 1]) { start = i } max = Math.max(max, i - start + 1) } return max };
leetcode : https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除