|
| 1 | +# 在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。 |
| 2 | +# |
| 3 | +# 现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足满足: |
| 4 | +# |
| 5 | +# |
| 6 | +# nums1[i] == nums2[j] |
| 7 | +# 且绘制的直线不与任何其他连线(非水平线)相交。 |
| 8 | +# |
| 9 | +# |
| 10 | +# 请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。 |
| 11 | +# |
| 12 | +# 以这种方法绘制线条,并返回可以绘制的最大连线数。 |
| 13 | +# |
| 14 | +# |
| 15 | +# |
| 16 | +# 示例 1: |
| 17 | +# |
| 18 | +# |
| 19 | +# 输入:nums1 = [1,4,2], nums2 = [1,2,4] |
| 20 | +# 输出:2 |
| 21 | +# 解释:可以画出两条不交叉的线,如上图所示。 |
| 22 | +# 但无法画出第三条不相交的直线,因为从 nums1[1]=4 到 nums2[2]=4 的直线将与从 nums1[2]=2 到 nums2[1]=2 的直线相 |
| 23 | +# 交。 |
| 24 | +# |
| 25 | +# |
| 26 | +# |
| 27 | +# 示例 2: |
| 28 | +# |
| 29 | +# |
| 30 | +# 输入:nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2] |
| 31 | +# 输出:3 |
| 32 | +# |
| 33 | +# |
| 34 | +# |
| 35 | +# 示例 3: |
| 36 | +# |
| 37 | +# |
| 38 | +# 输入:nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1] |
| 39 | +# 输出:2 |
| 40 | +# |
| 41 | +# |
| 42 | +# |
| 43 | +# |
| 44 | +# |
| 45 | +# 提示: |
| 46 | +# |
| 47 | +# |
| 48 | +# 1 <= nums1.length, nums2.length <= 500 |
| 49 | +# 1 <= nums1[i], nums2[j] <= 2000 |
| 50 | +# |
| 51 | +# |
| 52 | +# |
| 53 | +# Related Topics 数组 动态规划 |
| 54 | +# 👍 346 👎 0 |
| 55 | + |
| 56 | + |
| 57 | +# leetcode submit region begin(Prohibit modification and deletion) |
| 58 | +class Solution: |
| 59 | + def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int: |
| 60 | + l1 = len(nums1) |
| 61 | + l2 = len(nums2) |
| 62 | + dp = [[0] * (l2+1)for i in range(l1+1)] |
| 63 | + for i in range(1,l1+1): |
| 64 | + for j in range(1, l2+1): |
| 65 | + if nums1[i-1]==nums2[j-1]: |
| 66 | + dp[i][j] = dp[i-1][j-1]+1 |
| 67 | + else: |
| 68 | + dp[i][j] = max(dp[i][j-1],dp[i-1][j],dp[i-1][j-1]) |
| 69 | + return dp[-1][-1] |
| 70 | +# leetcode submit region end(Prohibit modification and deletion) |
0 commit comments