Dynamic Programming 1. 最长上升子序列 1 2 3 4 5 6 7 8 9 10 11 12 class Solution: def lengthOfLIS(self, nums: List[int]) -> int: if len(nums) == 0: return 0 dp = [1] * len(nums) L = len(nums) for i in range(L): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j]+1) return max(dp) 2. 最长公共子序列 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def longestCommonSubsequence(self, text1: str, text2: str) -> int: L1, L2 = len(text1), len(text2) dp = [[0] * (L1+1) for _ in range(L2+1)] for i in range(1, L2+1): for j in range(1, L1+1): if text2[i-1] == text1[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] 3. 三角形最小路……
阅读全文