Posted on 2023-05-13 19:44
Uriel 阅读(32)
评论(0) 编辑 收藏 引用 所属分类:
DP 、
闲来无事重切Leet Code
给定两个数字串,相同数字可以连线,问连线不交叉的话最多可以连几条线,DP,有参考Discussion
1 #1035
2 #Runtime: 112 ms (Beats 88.11%)
3 #Memory: 13.3 MB (Beats 97.36%)
4
5 class Solution(object):
6 def maxUncrossedLines(self, nums1, nums2):
7 """
8 :type nums1: List[int]
9 :type nums2: List[int]
10 :rtype: int
11 """
12 m, n = len(nums1), len(nums2)
13 dp = [0] * (n + 1)
14 for i in range(1, m + 1):
15 pre = 0
16 for j in range(1, n + 1):
17 cur = dp[j]
18 if nums1[i - 1] == nums2[j - 1]:
19 dp[j] = pre + 1
20 else:
21 dp[j] = max(dp[j-1], cur)
22 pre = cur
23 return dp[n]