(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

时间:2021-11-21 19:30:42

 

(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

这道题求连续的最长递增子序列。

思路:

1. 确定状态:

(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

2. 子问题:

(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

3. 状态方程:

(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

4. 初始条件及边界情况:

(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

5. 计算顺序:

(动态规划) leetcode 647. Longest Continuous Increasing Subsequence

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int n = nums.size();
        if(n==0)
            return 0;
        int f[n];
        int res = 0;
        for(int i=0; i<n; i++){
            f[i] = 1;
            if(i>0 && nums[i-1]<nums[i])
                f[i] = f[i-1]+1;
            
            if(f[i] > res)
                res = f[i];
        }
        
        return res;
    }
};