力扣算法 Java 刷题笔记【动态规划篇 DP 子序列类型问题】hot100(二) LIS 最长递增子序列及其变形 3

1. 最长递增子序列 LIS (中等)

地址: https://leetcode-cn.com/problems/longest-increasing-subsequence/
2021/12/26
做题反思:

class Solution {
    public int lengthOfLIS(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);

        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }

        int res = 0;
        for (int i = 0; i < n; i++) {
            res = Math.max(res, dp[i]);
        }

        return res;
    }
}

在这里插入图片描述

2. 俄罗斯套娃信封问题(困难)

(华为2021.8、新浪、微软)
地址: https://leetcode-cn.com/problems/russian-doll-envelopes/
2021/12/
做题反思:


3. 最大子数组和 (简单)

地址: https://leetcode-cn.com/problems/maximum-subarray/
2021/12/26
做题反思:

class Solution {
    public int maxSubArray(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        dp[0] = nums[0];

        for (int i = 1; i < n; i++) {
            dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
        } 

        int res = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            res = Math.max(res, dp[i]);
        }

        return res;
    }
}
posted @ 2022-03-04 23:03  涤心  阅读(29)  评论(0)    收藏  举报