[Swift]LeetCode1035.不相交的线 | Uncrossed Lines

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10783476.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

Now, we may draw a straight line connecting two numbers A[i] and B[j] as long as A[i] == B[j], and the line we draw does not intersect any other connecting (non-horizontal) 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 

 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数。

现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且我们绘制的直线不与任何其他连线(非水平线)相交。 

以这种方法绘制线条,并返回我们可以绘制的最大连线数。

示例 1:

输入:A = [1,4,2], B = [1,2,4]
输出:2
解释:
我们可以画出两条不交叉的线,如上图所示。
我们无法画出第三条不相交的直线,因为从 A[1]=4 到 B[2]=4 的直线将与从 A[2]=2 到 B[1]=2 的直线相交。

示例 2:

输入:A = [2,5,1,2,5], B = [10,5,2,1,5,2]
输出:3 

示例 3:

输入:A = [1,3,7,1,7,5], B = [1,9,2,5,1]
输出:2

提示: 

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

76ms
 1 class Solution {
 2     func maxUncrossedLines(_ A: [Int], _ B: [Int]) -> Int {
 3         var dp = Array(repeating: Array(repeating: 0, count: B.count), count: A.count)        
 4         var res = 0        
 5         for row in 0 ..< A.count {
 6             for col in 0 ..< B.count {
 7                 dp[row][col] = max(col > 0 ? dp[row][col - 1] : 0, row > 0 ? dp[row - 1][col] : 0)
 8                 if A[row] == B[col] {
 9                     dp[row][col] = max(dp[row][col], col > 0 && row > 0 ? dp[row - 1][col - 1] + 1 : 1)
10                     
11                     res = max(res, dp[row][col])
12                 }    
13             }
14         }
15         return res
16         
17     }
18 }

Runtime: 84 ms

Memory Usage: 18.7 MB 
 1 class Solution {
 2     func maxUncrossedLines(_ A: [Int], _ B: [Int]) -> Int {
 3         let n:Int = A.count
 4         let m:Int = B.count
 5         var dp:[[Int]] = [[Int]](repeating:[Int](repeating: 0, count:m + 1),count:n + 1)
 6         dp[0][0] = 0
 7         for i in 1...n
 8         {
 9             for j in 1...m
10             {
11                 if A[i - 1] == B[j - 1]
12                 {
13                     dp[i][j] = dp[i - 1][j - 1] + 1
14                 }
15                 else
16                 {
17                      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
18                 }
19             }
20         }
21         return dp[n][m]
22     }
23 }

92ms

 1 class Solution {
 2     func maxUncrossedLines(_ A: [Int], _ B: [Int]) -> Int {                
 3         var dp = [[Int]](repeating: [Int](repeating: 0, count: B.count+1), count: A.count+1)
 4         for i in 1...A.count {
 5             for j in 1...B.count {
 6                 if A[i - 1] == B[j - 1] {
 7                     dp[i][j] = 1 + dp[i - 1][j - 1]
 8                 } else {
 9                     dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
10                 }
11             }
12         }
13         return dp[A.count][B.count]
14     }
15 }

196ms

 1 class Solution {
 2     var dp = [[Int?]]()
 3     var arrA = [Int]()
 4     var arrB = [Int]()
 5     func maxUncrossedLines(_ A: [Int], _ B: [Int]) -> Int {
 6         arrA = A
 7         arrB = B
 8         dp = Array(repeating: Array(repeating: nil, count: B.count), count: A.count)
 9         
10         return helper(startA: 0, startB: 0)
11     }
12     
13     private func helper(startA: Int, startB: Int) -> Int {
14         if let result = dp[startA][startB] {
15             return result
16         }
17         
18         if startA == arrA.count - 1 && startB == arrB.count - 1 {
19             let result = arrA[startA] == arrB[startB] ? 1 : 0
20             dp[startA][startB] = result
21             return result
22         }
23         
24         if startA == arrA.count - 1 || startB == arrB.count - 1{
25             let a = arrA[startA]
26             let b = arrB[startB]
27             let result: Int
28             if a == b {
29                 result = 1
30             } else if startA == arrA.count - 1 {
31                 result = helper(startA: startA, startB: startB + 1)
32             } else {
33                 result = helper(startA: startA + 1, startB: startB)
34             }
35             
36             dp[startA][startB] = result
37             return result
38         }
39         
40         let a = arrA[startA]
41         let b = arrB[startB]
42         let result: Int
43         if a == b {
44             result = helper(startA: startA + 1, startB: startB + 1) + 1
45         } else {
46             result = max(
47                 helper(startA: startA + 1, startB: startB),
48                 helper(startA: startA, startB: startB + 1)
49             )
50         }
51         
52         dp[startA][startB] = result
53         return result
54     }
55 }

 

转载于:https://www.cnblogs.com/strengthen/p/10783476.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394012.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

BZOJ1054(搜索)

大力搜&#xff0c;状态用一个16位的数字表示。 1 #include <bits/stdc.h>2 3 using namespace std;4 5 #define rep(i,a,b) for(int i(a); i < (b); i)6 7 const int A 30 1;8 9 struct node{int x, y; } op[A]; 10 struct Nod…

php sql语句过滤,php如何做sql过滤

php如何做sql过滤SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序&#xff0c;而这些输入大都是SQL语法里的一些组合&#xff0c;通过执行SQL语句进而执行攻击者所要的操作&#xff0c;其主要原因是程序没有细致地过滤用户输入的数据&#xff0c;致使非法数据侵入…

ajaxfileupload 返回值_ajaxFileUpload上传文件返回json无法解析

最近做一个文件上传的功能&#xff0c;还要绑定数据传输到后台&#xff0c;为了不影响前端的体验&#xff0c;采用ajax发送请求。找了一些资料&#xff0c;网上的用ajaxupload这个插件。但是无论成功还是失败都是执行的error的回调函数。后台我采用springmvc返回的json&#xf…

leetcode133. 克隆图(bfs)

给你无向 连通 图中一个节点的引用&#xff0c;请你返回该图的 深拷贝&#xff08;克隆&#xff09;。 图中的每个节点都包含它的值 val&#xff08;int&#xff09; 和其邻居的列表&#xff08;list[Node]&#xff09;。 class Node { public int val; public List neighbor…

OSCON上最受欢迎的Docker演讲

本文讲的是OSCON上最受欢迎的Docker演讲&#xff0c;【编者的话】本文介绍了上个月OSCON大会有关Docker最受欢迎的一个分享&#xff1a;真实线上环境的Docker技巧。分享者是一名运维工程师叫Bridget&#xff0c;她所在的公司DramaFever在2013年10月开始在线上环境部署使用Docke…

测试驱动开发 测试前移_测试驱动开发:它是什么,什么不是。

测试驱动开发 测试前移by Andrea Koutifaris由Andrea Koutifaris Test driven development has become popular over the last few years. Many programmers have tried this technique, failed, and concluded that TDD is not worth the effort it requires.在过去的几年中&…

【C/C++开发】C++库大全

C特殊限定符(1)--static 当static来修饰类数据成员时&#xff0c;这个类的所有对象都可以访问它。因为值在内存中持续存在&#xff0c;它可以被对象有效共享。这意味着当一个对象改变static数据成员的值时&#xff0c;就改变了所有对象的这个数据成员的值。 定义一个类: class …

java二维数组水平翻转,C 语言 利用二维数组实现对输入的数组进行翻转

C 语言 利用二维数组实现对输入的数组进行翻转(帮助理解对图像翻转编辑原理)/*?输入几行几列数字和翻转方式&#xff0c;如&#xff1a;3 4 0即代表3行4列&#xff0c;左右翻转&#xff1b;6 5 1即代表6行5列&#xff0c;上下翻转。输入示例&#xff1a;3 4 0________________…

lightgbm 保存模型 过大_一个例子读懂LightGBM的模型文件

机器学习模型的可解释性是个让人头痛的问题。在使用LightGBM模型的肯定对生成的GBDT的结构是好奇的&#xff0c;我也好奇&#xff0c;所以就解析一个LightGBM的模型文件看看&#xff0c;通过这个解析&#xff0c;你可以看懂GBDT的结构。另外&#xff0c;了解模型文件&#xff0…

Oracle Sql 胡乱记

/Oracle查询优化改写/ --1、coalesce 返回多个值中&#xff0c;第一个不为空的值 select coalesce(, , s) from dual; --2、order by -----dbms_random.value 生产随机数,利用随机数对查询结果进行随机排序 select * from emp order by dbms_random.value; --指定查询结果中的一…

leetcode752. 打开转盘锁(bfs)

你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字&#xff1a; ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ 。每个拨轮可以自由旋转&#xff1a;例如把 ‘9’ 变为 ‘0’&#xff0c;‘0’ 变为 ‘9’ 。每次旋转都只能旋转一个拨轮的一位…

Object Pools 喷泉效果实现

摘录自&#xff1a;http://catlikecoding.com/unity/tutorials/object-pools/ 工程 效果图 工程里面有响应的注释 源码我就不单独放出来了

从头学习计算机网络_我如何通过从头开始构建网络爬虫来自动进行求职

从头学习计算机网络它是如何开始的故事 (The story of how it began) It was midnight on a Friday, my friends were out having a good time, and yet I was nailed to my computer screen typing away.星期五是午夜&#xff0c;我的朋友们出去玩得很开心&#xff0c;但我被钉…

php 动态生成文件,php动态程序生成静态文件示例

html>{title}{content}tmp.html是模板文件/** 说明&#xff1a;生成静态页面,tmp.html是模板文件&#xff0c;news.html是要生成的文件&#xff0c;**///1&#xff0c;先读取模板中内容$strfile_get_contents(tmp.html);//2&#xff0c;将指定的内容进行替换$title网站标题;…

网管的自我修养-网络系统

目录&#xff1a; 序章人际关系工具准备电脑维护网络系统弱电系统外设相关信息系统服务器相关机房建设其他网管网管&#xff0c;会管网络才算名副其实。管理一般中小企业的网络&#xff0c;具备CCNA及以上水平就可以了。 一、规划 首先要根据公司的人员工位数量、打印机传真等设…

thinkphp日志泄漏漏洞_ThinkPHP框架被爆任意代码执行漏洞

昨日ThinkPHP框架被爆出了一个php代码任意执行漏洞&#xff0c;黑客只需提交一段特殊的URL就可以在网站上执行恶意代码。ThinkPHP作为国内使用比较广泛的老牌PHP MVC框架&#xff0c;有不少创业公司或者项目都用了这个框架。不过大多数开发者和使用者并没有注意到本次漏洞的危害…

leetcode 113. 路径总和 II(Path Sum II)

目录 题目描述&#xff1a;示例:解法&#xff1a;题目描述&#xff1a; 给定一个二叉树和一个目标和&#xff0c;找到所有从根节点到叶子节点路径总和等于给定目标和的路径。 说明: 叶子节点是指没有子节点的节点。 示例: 给定如下二叉树&#xff0c;以及目标和 sum 22&#x…

VMware下配置固定ip,于本机进行通信。

虚拟机装好后&#xff0c;会生成虚拟的网络信息。点开VMware下虚拟网络编辑器。选择net模式的记录会发现设定好的网关及dns。 我们只需要在虚拟机上配好对于的ip 输入 dns 和网关即可转载于:https://blog.51cto.com/thlovesky/1967929

leetcode417. 太平洋大西洋水流问题(bfs)

给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界&#xff0c;而“大西洋”处于大陆的右边界和下边界。规定水流只能按照上、下、左、右四个方向流动&#xff0c;且只能从高到低或者在同等高度上流动。请找出那些水流既可以…

为什么测试喜欢ie_为什么我现在喜欢测试,以及为什么您也应该如此。

为什么测试喜欢ieby Evelyn Chan通过伊芙琳陈 为什么我现在喜欢测试&#xff0c;以及为什么您也应该如此。 (Why I now appreciate testing, and why you should, too.) There’s a common misconception that writing tests slows down development speed. While the benefit…