【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 ...
随机推荐
- GNU Makefile手札
通配符 $@ 目标集合 $< 第一个依赖文件 $^ 所有依赖文件 $? 执行结果 % 表示任意长度的字符 $$$$ 随机数 @<command> 执行时不输出该命令到控制台 变量赋值 ...
- linux 下spyder安装
linux 下spyder安装: 安装qt4,python3 对应qt5 sudo apt-get install libxext6 libxext-dev libqt4-dev libqt4-gu ...
- Django学习之路由系统
一.Django的路由系统 1.URLconf配置 基本格式: 参数说明: 2.正则表达式详解 基本配置 注意事项 补充说明 3.分组命名匹配 URLconf匹配的位置 捕获的参数永远都是字符串 视图 ...
- MySQL的常用JSON函数
1. JSON_SEARCH(col ->> '$[*].key', type, val) col: JSON格式的字段名 key:要搜索的col字段的key type:可以为'one'或 ...
- Python高效率遍历文件夹寻找重复文件
前言 为什么要写这篇文章呢...主要还是业务中有个需求,遍历一个将近200w数据的文件夹,大部分还都是视频文件那种,但是这玩意用的次数还不多,做文件夹index也不是很ok,所以写了一个脚本来处理这个 ...
- 使用fiddler,提示系统找不到相应的文件FSE2.exe文件
使用fiddler时候遇到了如下问题: Rules中customize rules 时,提示系统找不到相应的文件FSE2.exe文件. 这个文件的位置可以在Tools->opinions-> ...
- 【minicap】基于图像识别的手机端UI自动化测试的截图方式更新
minicap属于STF框架的一个工具,可以高速截图.同步手机屏幕至浏览器等 为什么用minicap截图: 经过试验,截同一个屏幕的一张图,adb shell screencap速度为2.9秒,而 ...
- scrapy爬取booking酒店评论数据
# scrapy爬取酒店评论数据 -- 代码 here:github地址:https://github.com/760730895/scrapy_Booking-- 采用scrapy爬取酒店评论数据 ...
- Tensorflow实战 手写数字识别(Tensorboard可视化)
一.前言 为了更好的理解Neural Network,本文使用Tensorflow实现一个最简单的神经网络,然后使用MNIST数据集进行测试.同时使用Tensorboard对训练过程进行可视化,算是打 ...
- ajax 的post方法 的content-type设置和express里应用body-parser
ajax的post方法相比get方法,在传参形式上很不一样, get把参数用'?'拼接在端口后,并且用'&'连接;而post则是需要在send参数里设置. 根据ajax实例xhr.setReq ...
