Algorithm/src/main/java/dynamic_programming/FindLengthOfLCIS.java
2025-04-17 12:08:47 +08:00

32 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dynamic_programming;
import java.util.Arrays;
/**
* 题目: 674. 最长连续递增序列 (MaxProduct)
* 描述:给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
* 连续递增的子序列 可以由两个下标 l 和 rl < r确定如果对于每个 l <= i < r都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] 就是连续递增子序列。
* 示例 1
输入nums = [1,3,5,4,7]
输出3
解释:最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
* 链接https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
*/
public class FindLengthOfLCIS {
public int findLengthOfLCIS(int[] nums) {
int sz=nums.length;
int[]dp=new int[sz];
int ans=1;
Arrays.fill(dp,1);
for (int i = 1; i < sz; i++) {
if(nums[i]>nums[i-1])
dp[i]=dp[i-1]+1;
ans=Math.max(ans,dp[i]);
}
return ans;
}
}