题目如下:

We write the integers of A and B (in the order they are given) on two separate horizontal lines.

Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that:

  • A[i] == B[j];
  • The line we draw does not intersect any other connecting (non-horizontal) line.

Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.

Return the maximum number of connecting lines we can draw in this way.

Example 1:

Input: A = [1,4,2], B = [1,2,4]
Output: 2
Explanation: We can draw 2 uncrossed lines as in the diagram.
We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.

Example 2:

Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2]
Output: 3

Example 3:

Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1]
Output: 2

Note:

  1. 1 <= A.length <= 500
  2. 1 <= B.length <= 500
  3. 1 <= A[i], B[i] <= 2000

解题思路:本题可以采用动态规划的方法。记dp[i][j]为A[i]与B[j]连线后可以组成的最多连线的数量,当然这里A[i]与B[j]连线是虚拟的连线,因此存在A[i] != B[j]的情况。首先来看A[i] == B[j],这说明A[i]与B[i]可以连线,显然有dp[i][j] = dp[i-1][j-1]+1;如果是A[i] != B[j],那么分为三种情况dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j]),这是因为A[i]不与B[j]连线,但是A[i]可能可以与B[j]之前所有点的连线,同理B[j]也是一样的。

代码如下:

class Solution(object):
def maxUncrossedLines(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
dp = []
for i in range(len(A)):
dp.append([0] * len(B)) for i in range(len(A)):
for j in range(len(B)):
if A[i] == B[j]:
dp[i][j] = max(dp[i][j],1)
if i - 1 >= 0 and j - 1 >= 0 :
dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1) else:
if i - 1 >= 0 and j - 1 >= 0:
dp[i][j] = max(dp[i][j],dp[i-1][j-1])
if j - 1 >= 0:
dp[i][j] = max(dp[i][j],dp[i][j-1])
if i - 1 >= 0:
dp[i][j] = max(dp[i][j],dp[i-1][j])
return dp[-1][-1]

【leetcode】1035. Uncrossed Lines的更多相关文章

  1. 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035

    package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...

  2. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. ThinkPHP框架实现rewrite路由配置

    rewrite路由形式:   //网址/分组/控制器/方法 配置实现rewrite路由的配置: 1. 修改apache的配置 先修改httpd.conf配置文件中的AllowOverrideAll,全 ...

  2. xcode archive灰色,无法打包的解决办法

    问题如图: 解决办法:目前的运行配置是使用模拟器,改成“iOS Device”即可 step1: step2: 修改后archive选项变为黑色,可点击状态了

  3. unittest框架扩展(基于代码驱动)自动化-下

    一.数据驱动/代码驱动优缺点: 使用数据驱动的好处:- 代码复用率高.同一测试逻辑编写一次,可以被多条测试数据复用,提高了测试代码的复用率,同时可以提高测试脚本的编写效率.- 异常排查效率高.测试框架 ...

  4. hbase的TTL机制清除opentsdb的超时数据

    我们发现用opentsdb向hbase写数据之后,磁盘占用率飙升得很快,我们存的业务数据只用保存一个月的即可,了解hbase的TTL机制可以清除相关表.相关行的超时数据,之前在数据备份时,我介绍了,o ...

  5. 【ABAP系列】SAP ABAP WRITE字段隐藏的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 字段隐藏的方法 ...

  6. Monte Carlo Policy Evaluation

    Model-Based and Model-Free In the previous several posts, we mainly talked about Model-Based Reinfor ...

  7. 应用安全 - 安全设备 - WAF原理/检测/绕过

    原理 基于Cookie值 Citrix Netscaler(2013年使用广泛) “Citrix Netscaler”会在HTTP返回头部Cookie位置加入“ns_af”的值,可以以此判断为Citr ...

  8. == 和 equals的区别

    == 和 equals的区别   基本类型:== 比较的是两个变量的面值大小 对象对象: 比较的是内存地址  特例: String a = "abc" String b = &qu ...

  9. 红黑树插入操作---以JDK 源码为例

    红黑树遵循的条件: 1.根节点为黑色. 2.外部节点(叶子节点)为黑色. 3.红色节点的孩子节点为黑色.(由此,红色节点的父节点也必为黑色) 4.从根节点到任一外部节点的路径上,黑节点的数量相同. 节 ...

  10. Docker 容器化部署1小时简单入门

    Docker简介 Docker是DotCloud开源的.可以将任何应用包装在Linux container中运行的工具.2013年3月发布首个版本,当前最新版本为1.3.Docker基于Go语言开发, ...