674. Longest Continuous Increasing Subsequence

网友投稿 733 2022-10-09

674. Longest Continuous Increasing Subsequence

674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:

Input: [1,3,5,4,7]Output: 3Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.

Example 2:

Input: [2,2,2,2,2]Output: 1Explanation: The longest continuous increasing subsequence is [2], its length is 1.

Note: Length of the array will not exceed 10,000.

思路: 用了DP记录当前序列的最大连续长度,dp[i] 表示当前位置的最大连续长度,更新dp[i] = dp[i - 1] + 1 if (前一元素小于当前元素)。

class Solution { public int findLengthOfLCIS(int[] nums) { if (nums.length == 0) return 0; int[] dp = new int[nums.length]; Arrays.fill(dp, 1); int max = 1; for (int i = 1; i < nums.length; ++i) { if (nums[i] > nums[i - 1]) { dp[i] = dp[i - 1] + 1; } max = Math.max(max, dp[i]); } return max; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:CCF 集合竞价
下一篇:We重邮 - 微信小程序(we重邮下载)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~