youngyangyang04 / leetcode-master-comment

用来做评论区
0 stars 0 forks source link

[Vssue]0674.最长连续递增序列.md #145

Open youngyangyang04 opened 3 weeks ago

youngyangyang04 commented 3 weeks ago

https://www.programmercarl.com/0674.%E6%9C%80%E9%95%BF%E8%BF%9E%E7%BB%AD%E9%80%92%E5%A2%9E%E5%BA%8F%E5%88%97.html

Du1in9 commented 5 days ago
if(nums.length <= 1) return nums.length;
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);

int result = 1;
for (int i = 1; i < nums.length; i++) {
    if (nums[i] > nums[i - 1]) {
        dp[i] = dp[i - 1] + 1;
    }
    if (dp[i] > result) result = dp[i];
}
return result;
// 例: nums = [1,3,5,4,7], 面试高频题.
i = 1: 满足 3 > 1, 则 dp[1] = 2 (子序列 [1,3]), result = 2
i = 2: 满足 5 > 3, 则 dp[2] = 3 (子序列 [1,3,5]), result = 3
i = 3: 不满足 4 > 5, 则继续遍历 (子序列 [4]), result = 3
i = 4: 满足 7 > 4, 则 dp[4] = 2 (子序列 [4,7]), result = 3