1143. Longest Common Subsequence
1143. Longest Common Subsequence
上課會學到的經典動態規劃問題,最長公共子序列 。
同時從兩個字字串的開頭出發
如果說兩個字元一樣,那代表有一個公共子序列,繼續檢查兩個字串的下一個位子
如果說兩個字元不一樣,那就分別先移動第一個字串指針,繼續第二個字串相比,或是移動第二個字串的指針,繼續第一個字串相比,再看哪一個子問題有最優解。
遞迴加上記憶法
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
@lru_cache(maxsize=None)
def helper(i, j):
if i == len(text1) or j == len(text2):
return 0
if text1[i] == text2[j]:
return 1 + helper(i + 1, j + 1)
else:
return max(helper(i + 1, j), helper(i, j + 1))
return helper(0, 0)不用 Python 的 lru_cache
動態規劃自底向上
再更節省記憶體空間
Last updated