Day35 | 300最长上升子序列&1143最长公共子序列&1035不相交的线&674最长连续递增序列&718最长重复子数组&53最大子数组和

103 阅读2分钟

最长上升子序列 LeetCode 300

题目链接:[LeetCode 300 - 中等]

思路

动态规划 具体思路斟酌

动态规划:

class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        Arrays.fill(dp,1);
        int res = 1;
        for(int i=1;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;
    }
}

最长公共子序列 LeetCode 1143

题目链接:[LeetCode 1143 - 中等]

思路

动态规划 具体思路斟酌

动态规划:

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int[][] dp = new int[text1.length()+1][text2.length()+1];
        
        for(int i = 1;i <= text1.length();i++){
            char ch1 = text1.charAt(i-1);
            for(int j = 1;j <= text2.length();j++){
                char ch2 = text2.charAt(j-1);
                if(ch1==ch2){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else{
                    dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        return dp[text1.length()][text2.length()];
    }
}

不相交的线 LeetCode 1035

题目链接:[LeetCode 1035 - 中等]

思路

动态规划:

class Solution {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int[][] dp = new int[nums1.length+1][nums2.length+1];
        for(int i=1;i<=nums1.length;i++){
            for(int j=1;j<=nums2.length;j++){
                if(nums1[i-1]==nums2[j-1]){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else{
                    dp[i][j]=Math.max(dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        return dp[nums1.length][nums2.length];
    }
}

最长连续递增序列 LeetCode 674

题目链接:[LeetCode 674 - 简单]

思路

动态规划:

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int res = 1;
        int count = 1;
        for(int i=1;i<nums.length;i++){
            if(nums[i]>nums[i-1]){
                count++;
            }else{
                count=1;
            }
            res = Math.max(res,count);
        }
        return res;
    }
}

最长重复子数组 LeetCode 718

题目链接:[LeetCode 718 - 中等]

思路

① 初始值为 res = 0

② 递推公式 dp[i][j] = dp[i-1][j-1]+1

③ 遍历顺序 没有固定要求

动态规划:

class Solution {
    public int findLength(int[] nums1, int[] nums2) {
        int[][] dp = new int[nums1.length+1][nums2.length+1];
        int res = 0;
        for(int i=1;i<=nums1.length;i++){
            for(int j=1;j<=nums2.length;j++){
                if(nums1[i-1]==nums2[j-1]){
                    dp[i][j] = dp[i-1][j-1]+1;
                    res = Math.max(dp[i][j],res);
                }
            }
        }
        return res;
    }
}

最大子数组和 LeetCode 53

题目链接:[LeetCode 53 - 中等]

思路

动态规划:

class Solution {
    public int maxSubArray(int[] nums) {
        int[] dp = new int[nums.length];
        dp[0]=nums[0];
        int res = nums[0];
        for(int i=1;i<nums.length;i++){
            dp[i]=Math.max(dp[i-1]+nums[i],nums[i]);
            res=Math.max(dp[i],res);
        }
        return res;
    }
}