当前位置:   article > 正文

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

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 subsequenceIf there is no common subsequence, return 0.

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".

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 not the 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)

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

 

1-dimensional DP:

Time complexity: O(m x n)

Space complexity: O(m)

  1. class Solution:
  2. def longestCommonSubsequence(self, text1: str, text2: str) -> int:
  3. m, n = len(text1), len(text2)
  4. dp = [0] * (n + 1) # 初始化一维DP数组
  5. for i in range(1, m + 1):
  6. prev = 0 # 保存上一个位置的最长公共子序列长度
  7. for j in range(1, n + 1):
  8. curr = dp[j] # 保存当前位置的最长公共子序列长度
  9. if text1[i - 1] == text2[j - 1]:
  10. # 如果当前字符相等,则最长公共子序列长度加一
  11. dp[j] = prev + 1
  12. else:
  13. # 如果当前字符不相等,则选择保留前一个位置的最长公共子序列长度中的较大值
  14. dp[j] = max(dp[j], dp[j - 1])
  15. prev = curr # 更新上一个位置的最长公共子序列长度
  16. 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.

  1. class Solution:
  2. def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
  3. m = len(nums1)
  4. n = len(nums2)
  5. dp = [[0] * (n + 1) for _ in range(m + 1)]
  6. for i in range(1, m + 1):
  7. for j in range(1, n + 1):
  8. if nums1[i - 1] == nums2[j - 1]:
  9. dp[i][j] = dp[i - 1][j - 1] + 1
  10. else:
  11. dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
  12. 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:

  1. class Solution:
  2. def maxSubArray(self, nums: List[int]) -> int:
  3. dp = [float('-inf')] * len(nums) # 不能 -inf
  4. dp[0] = nums[0]
  5. result = dp[0] #初始化 ,必须要有 ,不能直接max(dp)
  6. for i in range(1, len(nums)):
  7. dp[i] = max(nums[i], dp[i - 1] + nums[i]) # 不是dp[i]是num[i] !!!!!!!!!!
  8. if dp[i] > result:
  9. result = dp[i]
  10. return result

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/241177
推荐阅读
相关标签
  

闽ICP备14008679号