【算法刷题day53】Leetcode:1143. 最长公共子序列、1035. 不相交的线、53. 最大子数组和

文章目录

草稿图网站
java的Deque

Leetcode 1143. 最长公共子序列

题目: 1143. 最长公共子序列
解析: [代码随想录解析](https://programmercarl.com/1143.最长公共子序列.html

解题思路

和上一题的区别是,不初始化0,如果没匹配到就变为左边数

代码

java 复制代码
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int res = 0;
        int [][]dp = new int[text1.length()+1][text2.length()+1];
        for (int i = 1; i <= text1.length(); i++) {
            for (int j = 1; j <= text2.length(); j++) {
                if (text1.charAt(i-1) == text2.charAt(j-1))
                    dp[i][j] = dp[i-1][j-1] + 1;
                else
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                res = Math.max(res, dp[i][j]);
            }
        }
        return res;
    }
}

//滚动数组
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int res = 0;
        int []dp = new int[text2.length()+1];
        for (int i = 1; i <= text1.length(); i++) {
            for (int j = text2.length(); j > 0; j--) {
                if (text1.charAt(i-1) == text2.charAt(j-1))
                    dp[j] = dp[j-1] + 1;
                else
                    dp[j] = Math.max(dp[j], dp[j-1]);
                res = Math.max(res, dp[j]);
            }
        }
        return res;
    }
}

总结

暂无

Leetcode 1035. 不相交的线

题目: 1035. 不相交的线
解析: 代码随想录解析

解题思路

上秒那题包了个马甲

代码

java 复制代码
class Solution {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int res = 0;
        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-1][j], dp[i][j-1]);
                res = Math.max(res, dp[i][j]);
            }
        }
        return res;
    }
}

总结

暂无

Leetcode 53. 最大子数组和

题目: 53. 最大子数组和
解析: 代码随想录解析

解题思路

dp数组的含义是到这位置的最大和是多少

代码

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

总结

暂无

相关推荐
CoovallyAIHub2 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP3 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo3 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo3 小时前
300:最长递增子序列
算法
CoovallyAIHub8 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub9 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js