算法D53 | 动态规划14 | 1143.最长公共子序列 1035.不相交的线 53. 最大子序和 动态规划

1143.最长公共子序列

体会一下本题和 718. 最长重复子数组 的区别

视频讲解:https://www.bilibili.com/video/BV1ye4y1L7CQ

https://programmercarl.com/1143.最长公共子序列.html

Python:

python 复制代码
class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        n, m = len(text1), len(text2)
        dp = [[0]*(m+1) for _ in range(n+1)]
        result = 0
        for i in range(1, n+1):
            for j in range(1, m+1):
                if text1[i-1] == text2[j-1]:
                    dp[i][j] = dp[i-1][j-1] + 1
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
            result = max(result, dp[i][j])
        return result
        

C++:

cpp 复制代码
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        vector<vector<int>> dp(text1.size()+1, vector<int>(text2.size()+1, 0));
        int result = 0;
        for (int i=1; i<=text1.size(); i++) {
            for (int j=1; j<=text2.size(); j++) {
                if (text1[i-1]==text2[j-1]) {
                    dp[i][j] = dp[i-1][j-1]+1;
                } else {
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
                result = max(result, dp[i][j]);
            }
        }
        return result;
    }
};

1035.不相交的线

其实本题和 1143.最长公共子序列 是一模一样的,大家尝试自己做一做。

视频讲解:动态规划之子序列问题,换汤不换药 | LeetCode:1035.不相交的线_哔哩哔哩_bilibili

代码随想录

Python:

python 复制代码
class Solution:
    def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
        n, m = len(nums1), len(nums2)
        dp = [[0]*(m+1) for _ in range(n+1)]
        for i in range(1, n+1):
            for j in range(1, m+1):
                if nums1[i-1]==nums2[j-1]:
                    dp[i][j] = dp[i-1][j-1] + 1
                else:
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
        return dp[n][m]

C++:

cpp 复制代码
class Solution {
public:
    int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
        vector<vector<int>> dp(nums1.size()+1, vector<int>(nums2.size()+1, 0));
        for (int i=1; i<=nums1.size(); i++) {
            for (int j=1; j<=nums2.size(); j++) {
                if (nums1[i-1]==nums2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[nums1.size()][nums2.size()];
    }
};

53. 最大子序和

这道题我们用贪心做过,这次 再用dp来做一遍

视频讲解:看起来复杂,其实是简单动态规划 | LeetCode:53.最大子序和_哔哩哔哩_bilibili

代码随想录

Python:

python 复制代码
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        dp = [0]*len(nums)
        dp[0] = nums[0]
        result = nums[0]
        for i in range(1, len(nums)):
            dp[i] = max(dp[i-1]+nums[i], nums[i])
            result = max(result, dp[i])
        return result

C++:

cpp 复制代码
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        vector<int> dp(nums.size(), 0);
        dp[0] = nums[0];
        int result = nums[0];
        for (int i=1; i<nums.size(); i++) {
            dp[i] = max(dp[i-1]+nums[i], nums[i]);
            result = max(result, dp[i]);
        }
        return result;        
    }
};
相关推荐
loser~曹2 分钟前
基于快速排序解决 leetcode hot215 查找数组中第k大的数字
数据结构·算法·leetcode
啊阿狸不会拉杆5 分钟前
第二十一章:Python-Plotly库实现数据动态可视化
开发语言·python·plotly
Dream it possible!8 分钟前
LeetCode 热题 100_打家劫舍(83_198_中等_C++)(动态规划)
c++·算法·leetcode·动态规划
月走乂山12 分钟前
nocobase + Python爬虫实现数据可视化
爬虫·python·低代码·信息可视化
zhouziyi070113 分钟前
【蓝桥杯14天冲刺课题单】Day 8
c++·算法·蓝桥杯
滴答滴答嗒嗒滴16 分钟前
Python小练习系列 Vol.12:学生信息排序(sorted + key函数)
开发语言·python
SylviaW0817 分钟前
python-leetcode 62.搜索插入位置
数据结构·算法·leetcode
丶Darling.18 分钟前
26考研 | 王道 | 数据结构 | 第四章 串
数据结构·考研·kmp
愚润求学1 小时前
【C++】vector常用方法总结
开发语言·c++·vector
天天进步20151 小时前
Python项目-基于Flask的个人博客系统设计与实现(1)
开发语言·python·flask