0314-2020-LEETCODE-300- rise longest sequence - classic problem DP

Maintaining the DP [] array, by a state transition equation: dp [i] = Math.max (dp [i], dp [j] +1); When implemented when the state transition equation num [j] nums [i]> when the condition is not satisfied, skip this time j, does not operate. cycle interval is j for (0, i);

	//经典的动态规划问题:DP解决。
    public int lengthOfLIS1(int[] nums) {
        if (nums.length == 0){
            return 0;
        }
        int[] dp = new int[nums.length];
        Arrays.fill(dp,1);
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]){
                    dp[i] = Math.max(dp[i],dp[j] + 1);
                }
            }
            res = Math.max(res,dp[i]);
        }
        return res;
    }
Published 98 original articles · won praise 0 · Views 2199

Guess you like

Origin blog.csdn.net/weixin_43221993/article/details/104869080