1143. 最长公共子序列
- 思路
- example
- Longest Common Subsequence (LCS)
- 双串,二维DP, 不需要连续
dp[i][j]: text1 前i个,text2前j个 结果,i.e., text1: 0,...,i-1; text2: 0,...,j-1
注意对dp[i][j], 对应的最长公共子序列并不一定以i-1th 和 j-1th 结尾。
方便初始化处理, i.e., dp[0][j] = dp[i][0] = 0
Goal: dp[m][n]
- 复杂度. 时间:O(mn), 空间: O(mn)
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+1):
if text1[i-1] == text2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])
return dp[m][n]
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+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])
return dp[m][n]
- 可空间优化
1035. 不相交的线
- 思路
- example
- 本质就是“最长公共子序列”问题, 不需要连续
- 复杂度. 时间:O(mn), 空间: O(mn)
class Solution:
def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
dp = [[0 for _ in range(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][j-1], dp[i-1][j])
return dp[m][n]
class Solution:
def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
dp[0][0] = 0
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[m][n]
53. 最大子数组和
- 思路
- example
-
连续子数组
- 滑窗会比较麻烦?
- DP
dp[i]: 以ith 结尾的最大连续子数组和
目标: max(dp)
if dp[i-1] < 0: dp[i] = nums[i]
else: dp[i] = dp[i-1] + nums[i]
- 复杂度. 时间:O(n), 空间: O(n)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
dp = [0 for _ in range(n)]
dp[0] = nums[0]
res = dp[0]
for i in range(1, n):
if dp[i-1] > 0:
dp[i] = dp[i-1] + nums[i]
else:
dp[i] = nums[i]
res = max(res, dp[i])
return res
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
dp = [0 for _ in range(n)]
dp[0] = nums[0]
for i in range(1, n):
if dp[i-1] >= 0:
dp[i] = dp[i-1] + nums[i]
else:
dp[i] = nums[i]
return max(dp)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
sum_ = nums[0]
res = sum_
for i in range(1, n):
if sum_ < 0:
sum_ = nums[i]
else:
sum_ += nums[i]
res = max(res, sum_)
return res
小结
- 1维,2维,?
- 前i个 或者 以ith结尾,?
- 目标
583. 两个字符串的删除操作
- 思路
- example
双串, dp[i][j]
dp[i][j]: word1[0,...,i-1], word2[0,...,j-1]. 前i个,前j个
- 复杂度. 时间:O(mn), 空间: O(mn)
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
for j in range(n+1): # !!!
dp[0][j] = j
for i in range(m+1): # !!!
dp[i][0] = i
for i in range(1, m+1):
for j in range(1, n+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1
return dp[m][n]
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
for j in range(1, n+1):
dp[0][j] = j
for i in range(1, m+1):
dp[i][0] = i
for i in range(1, m+1):
for j in range(1, n+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1
return dp[m][n]
712. 两个字符串的最小ASCII删除和
- 思路
- example
双串
ASCII: ord('a')
- 复杂度. 时间:O(mn), 空间: O(mn)
class Solution:
def minimumDeleteSum(self, s1: str, s2: str) -> int:
m, n = len(s1), len(s2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
for j in range(1, n+1):
dp[0][j] = dp[0][j-1] + ord(s2[j-1])
for i in range(1, m+1):
dp[i][0] = dp[i-1][0] + ord(s1[i-1])
for i in range(1, m+1):
for j in range(1, n+1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j]+ord(s1[i-1]), dp[i][j-1]+ord(s2[j-1]))
return dp[m][n]
class Solution:
def minimumDeleteSum(self, s1: str, s2: str) -> int:
m, n = len(s1), len(s2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
for j in range(1, n+1):
dp[0][j] = dp[0][j-1] + ord(s2[j-1])
for i in range(1, m+1):
dp[i][0] = dp[i-1][0] + ord(s1[i-1])
for i in range(1, m+1):
for j in range(1, n+1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j]+ord(s1[i-1]), dp[i][j-1]+ord(s2[j-1]))
return dp[m][n]