1143. Longest Common Subsequence 1035. Uncrossed Lines 53. Maximum Subarray

1143. Longest Common Subsequence

Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence , return 0.

A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.

  • For example, "ace" is a subsequence of "abcde".

A common subsequence of two strings is a subsequence that is common to both strings.

There are two main cases to determine the recursive formula:

  1. text1[i - 1] is the same as text2[j - 1]

  2. text1[i - 1] is notthe same as text2[j - 1].

If text1[i - 1] and text2[j - 1] are the same, then a common element is found, so dp[i][j] = dp[i - 1][j - 1] + 1;

If text1[i - 1] and text2[j - 1] are not the same, then look at the longest common subsequence of text1[0, i - 2] and text2[0, j - 1] and the longest common subsequence of text1[0, i - 1] and text2[0, j - 2] and take the largest. i.e., dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

2-dimensional DP:

Time complexity: O(m x n)

Space complexity: O(m x n)

python 复制代码
class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        # 创建一个二维数组 dp,用于存储最长公共子序列的长度
        dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
        
        # 遍历 text1 和 text2,填充 dp 数组
        for i in range(1, len(text1) + 1):
            for j in range(1, len(text2) + 1):
                if text1[i - 1] == text2[j - 1]:
                    # 如果 text1[i-1] 和 text2[j-1] 相等,则当前位置的最长公共子序列长度为左上角位置的值加一
                    dp[i][j] = dp[i - 1][j - 1] + 1
                else:
                    # 如果 text1[i-1] 和 text2[j-1] 不相等,则当前位置的最长公共子序列长度为上方或左方的较大值
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
        
        # 返回最长公共子序列的长度
        return dp[len(text1)][len(text2)]

1-dimensional DP:

Time complexity: O(m x n)

Space complexity: O(m)

python 复制代码
class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        m, n = len(text1), len(text2)
        dp = [0] * (n + 1)  # 初始化一维DP数组
        
        for i in range(1, m + 1):
            prev = 0  # 保存上一个位置的最长公共子序列长度
            for j in range(1, n + 1):
                curr = dp[j]  # 保存当前位置的最长公共子序列长度
                if text1[i - 1] == text2[j - 1]:
                    # 如果当前字符相等,则最长公共子序列长度加一
                    dp[j] = prev + 1
                else:
                    # 如果当前字符不相等,则选择保留前一个位置的最长公共子序列长度中的较大值
                    dp[j] = max(dp[j], dp[j - 1])
                prev = curr  # 更新上一个位置的最长公共子序列长度
        
        return dp[n]  # 返回最后一个位置的最长公共子序列长度作为结果

1035. Uncrossed Lines

You are given two integer arrays nums1 and nums2. We write the integers of nums1 and nums2 (in the order they are given) on two separate horizontal lines.

We may draw connecting lines: a straight line connecting two numbers nums1[i] and nums2[j] such that:

  • nums1[i] == nums2[j], and
  • the line we draw does not intersect any other connecting (non-horizontal) line.

Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).

Return the maximum number of connecting lines we can draw in this way.

Its literaly like to get longest common subsequence from "adb" and "abd"

It's exactly the same as the last question.

python 复制代码
class Solution:
    def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
        m = len(nums1)
        n = len(nums2)

        dp = [[0] * (n + 1) for _ in range(m + 1)]
       
        for i in range(1, m + 1):
            for j in range(1, n + 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[-1][-1]

53. Maximum Subarray

Given an integer array nums, find the subarray with the largest sum, and return its sum.

第二次还没ac 老了老了

dp:

python 复制代码
class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        dp = [float('-inf')] * len(nums) # 不能 -inf
        dp[0] = nums[0]
        result = dp[0]  #初始化 ,必须要有 ,不能直接max(dp)

        for i in range(1, len(nums)):
            dp[i] = max(nums[i], dp[i - 1] + nums[i]) # 不是dp[i]是num[i] !!!!!!!!!!

            if dp[i] > result:
                result = dp[i]
        
        return result
相关推荐
P.H. Infinity18 分钟前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天22 分钟前
java的threadlocal为何内存泄漏
java
caridle33 分钟前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
萧鼎36 分钟前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
学地理的小胖砸37 分钟前
【一些关于Python的信息和帮助】
开发语言·python
疯一样的码农38 分钟前
Python 继承、多态、封装、抽象
开发语言·python
^velpro^39 分钟前
数据库连接池的创建
java·开发语言·数据库
苹果醋342 分钟前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端1 小时前
第六章 7.0 LinkList
java·开发语言·网络