【leetcode】1035. Uncrossed Lines
题目如下:
We write the integers of
AandB(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]andB[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: 3Example 3:
Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1]
Output: 2Note:
1 <= A.length <= 5001 <= B.length <= 5001 <= 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的更多相关文章
- 【LEETCODE】56、数组分类,适中级别,题目:62、63、1035
package y2019.Algorithm.array.medium; /** * @ClassName UniquePathsWithObstacles * @Description TODO ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- DAY 6 TEST
test T1 样例输入 样例输出 答案选择u,v作为关键点 暴力的话k^2枚举跑最短路,寻找最小值就行了 50pts 考虑优化枚举量 因为答案的两个点是不同的点,所以编号的二进制表示中至少一位不同 ...
- websocket 无需通过轮询服务器的方式以获得响应 同步在线用户数 上线下线 抓包 3-way-handshake web-linux-shell 开发
https://code.google.com/archive/p/phpwebsocket/source/default/source The WebSocket API (WebSockets) ...
- Fragment 基础使用及重叠问题
一 基本使用 Fragment依附于Activity使用,方面我们在一个页面里面切换显示多屏内容. Activity管理Fragment有两种方式,通过FragmentTransacation这个类来 ...
- 二十三、python中的time和datetime模块
A.time模块 1. sleep():强制等待 import timeimport datetime print("start to sleep.....")time.sle ...
- fedora23安装php,mysql
httpd: 他的服务器根: ServerRoot, 是在/etc/httpd. 因为httpd所有的配置文件, 运行文件等都在这里.所以这是他的根. httpd的配置文件: httpd.conf恰好 ...
- 【HTML】<!DOCTYPE html>作用
1.定义: DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档. <!DOCTYPE> 声明 ...
- 像计算机科学家一样思考python-第4章 案例研究:接口设计
系统环境 ubuntu18 4.1turtle模块 模块一开始导入turtle模块就报错了 Python ( , ::) [GCC ] on linux Type "help", ...
- 阶段1 语言基础+高级_1-2 -面向对象和封装_16this关键字的作用
this主要是在重名的情况下 ,起到区分的效果 新建demo04的包,里面新建类Person 通过this.进行区分 this关键字可以解决重名 分不开的问题 这里的person调用的sayHello ...
- charles之抓取浏览器https请求
用charles抓取浏览器https的包时,请求显示为unknown,且请求和响应数据乱码,本篇介绍如何抓取正常响应的https请求 目录 1.安装charles 2.安装证书.添加域名 3.抓包 1 ...
- java如何台生成二维码详解
现在呢说明页面上展示二维码的两种方式: 1.使用img标签的src来请求生成二维码,后台会直接返回: 2.此处跟上方意思相似,获取到url给img标签设置src属性: 特别注意:如果url有amp;, ...
