1 class Solution:
 2     def maxUncrossedLines(self, A: 'List[int]', B: 'List[int]') -> int:
 3         m = len(A)
 4         n = len(B)
 5         dp = [[0 for a in range(n+1)] for b in range(m+1)]
 6         for i in range(1,m+1):
 7             for j in range(1,n+1):
 8                 if A[i-1] == B[j-1]:
 9                     dp[i][j] = dp[i-1][j-1] + 1
10                 else:
11                     if dp[i][j-1] >= dp[i-1][j]:
12                         dp[i][j] = dp[i][j-1]
13                     else:
14                         dp[i][j] = dp[i-1][j]
15         return dp[m][n]

这道题的思路是,最长公共子序列LCS,典型的DP类型题目。我最怕的类型了。

posted on 2019-04-28 13:23  Sempron2800+  阅读(308)  评论(0编辑  收藏  举报