[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 <= A.length <= 500
1 <= B.length <= 500
- 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 <= A.length <= 500
1 <= B.length <= 500
1 <= A[i], B[i] <= 2000
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
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
[Swift]LeetCode1035.不相交的线 | Uncrossed Lines相关推荐
- 最长重复子数组最长公共子序列不相交的线
引言 这同样是两种类型的题目,有很多相似的地方和不同的地方,区别依然是连续和不连续之分. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: ...
- leetcode(力扣) 718. 最长重复子数组 1143. 最长公共子序列 1035. 不相交的线 (动态规划)
文章目录 718. 最长重复子数组 题目描述 思路分析 完整代码 1143. 最长公共子序列 1035. 不相交的线: 这三道题思路基本一样,尤其是最后两道,代码都一样,一点不需要改的,所以放一起把. ...
- leetcode 1035. 不相交的线(dp)
在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数. 现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足满足: nums1[i ...
- LeetCode 1035. 不相交的线(最长公共子序列DP)
文章目录 1. 题目 2. 解题 1. 题目 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数. 现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == ...
- 1035 不相交的线
题目描述: 我们在两条独立的水平线上按给定的顺序写下 A 和 B 中的整数. 现在,我们可以绘制一些连接两个数字 A[i] 和 B[j] 的直线,只要 A[i] == B[j],且我们绘制的直线不与任 ...
- 米哈游 2020 年实习生招聘笔试编程题1——最大不相交连线数
题目描述 有A.B两个数组,相同数字可以连成一条线,求两个数组中相同数字不相交的最大连线数. 思路分析 求两个数组中相同数字不相交的最大连线数,其实就是求两个数组的最长公共子序列的长度.很明显,这是一 ...
- 文巾解题 1035. 不相交的线
1 题目描述 2 解题思路 k条互不相交的直线分别连接了数组 nums1和nums2的 k 对相等的元素,而且这 k 对相等的元素在两个数组中的相对顺序是一致的,因此,这 k 对相等的元素组成的序列即 ...
- swift中performSegue连线的使用storyboard加载多个控制器方法和demo下载
文章目录 在storyboard中创建连线 使用连线 连线本控制器跳转 连线其他控制器跳转 storyboard下面多个控制器加载方法 获取箭头指向的控制器 一个storyboard多个控制器,获取其 ...
- LeetCode 1035 不相交的线
题目链接:力扣 问题转换为求最长公共子序列问题: 代码: class Solution { public:int maxUncrossedLines(vector<int>& nu ...
最新文章
- How does SGD weight_decay work?
- pyinstaller打包教程及错误RuntimeError: Unable to open ./shape_predictor_68_face_landmarks.dat
- Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
- day3 python 函数
- 狄克斯特拉(Dijkstra)算法原理详细解释与实现(python)
- 计算机硬件:固态硬盘选购的技巧
- python (第八章)补充-可迭代对象(补充高阶函数,以及常用的高阶函数)
- ovs加dpdk出现EAL No free hugepages reported in hugepages-1048576kB
- 启动车子温车_什么是冷车启动
- DM 源码阅读系列文章(四)dump/load 全量同步的实现
- Win7性能优化:解决多核处理器兼容问题
- 例如微博表情添加到textView中
- 500个运营工具大全,速度收藏!!!
- 数织游戏中的程序思维和数织的程序解法
- 电脑提示Wtautoreg.exe无法找到入口怎么解决?
- Linux中光盘使用的文件类型,Linux光盘行动之制作光盘
- julia常用矩阵函数_Julia 多维数组
- 在MATLAB中采用M文件实现对Simulink中的S函数程序实现自动调参数
- 教你一个图片快速取反色的方法
- Balser相机连接以后,采集图像失败