LC 718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Example 1:
Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].
Note:
- 1 <= len(A), len(B) <= 1000
- 0 <= A[i], B[i] < 100
Runtime: 78 ms, faster than 40.98% of Java online submissions for Maximum Length of Repeated Subarray.
class Solution {
public int findLength(int[] A, int[] B) {
int[][] dp = new int[A.length+1][B.length+1];
int ret = 0;
for(int i=1; i<dp.length; i++){
for(int j=1; j<dp[0].length; j++){
if(A[i-1] == B[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
ret = Math.max(ret, dp[i][j]);
}
}
}
return ret;
}
}
a better solution
Runtime: 26 ms, faster than 99.59% of Java online submissions for Maximum Length of Repeated Subarray.
有两个关键点,
第一个是内层循环从后往前遍历,如果从前往后遍历就是错误的,因为我们每一次更新dp的时候是dp[j+1] = dp[j] + 1,所以在更新j+1的时候要用到j的信息,而这个j应该是之前的j,也就是上一行的j,可以参考上面一个解法中矩阵的上一行。
第二个是如果没有匹配到,应该把j+1变成0,否则会产生错误的计数。
class Solution {
public int findLength(int[] A, int[] B) {
int[] dp = new int[A.length+1];
int max = 0;
for(int i=0; i<A.length; i++) {
for(int j=B.length-1; j>=0; j--) {
if (A[i] == B[j]) {
dp[j+1] = dp[j] + 1;
if (max < dp[j+1]) {
max = dp[j+1];
}
} else {
dp[j+1] = 0;
}
}
}
return max;
}
}
LC 718. Maximum Length of Repeated Subarray的更多相关文章
- Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray
714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- [LeetCode]Maximum Length of Repeated Subarray
Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...
- [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- [Swift]LeetCode718. 最长重复子数组 | Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- [leetcode-718-Maximum Length of Repeated Subarray]
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
随机推荐
- Jmeter服务器压力测试使用说明
Jmeter服务器压力测试使用说明 Apache JMeter是Apache组织开发的基于Java的压力测试工具. 官方地址:http://jmeter.apache.org/download_jme ...
- 解决No module named 'sklearn.cross_validation'
sklearn中已经废弃cross_validation,将其中的内容整合到model_selection中 将sklearn.cross_validation 替换为 sklearn.model_s ...
- Python Scrapy爬虫框架之初次使用
此篇博客为本人对小甲鱼的课程的总结. 关于Scrapy的安装网上都有方法,这里便不再叙述. 使用Scrapy抓取一个网站一共需要四个步骤: 0.创建一个Scrapy项目: 1.定义Item容器: 2. ...
- 团队第三次作业:Alpha版本发布
这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业要求链接 团队名称 众志陈成 这个作业的目标 通过团队协作了解软件开发的大致流程,并在这个过程中体会调整与优化程序的方法,为以后真实的软件开发奠 ...
- withdraw搭建
<!-- yuan -->首先得安装rabbitmq(由于运行过程中报错)1.安装rabbitmq服务软件包 : apt install rabbitmq-server2.安装完成后在ra ...
- PHP 按照指定数量分割数组
<?php /** * 系统辅助类 * @date 2019年7月2日 * @comment * */ class SystemUtils { private static $_instance ...
- Vue入门(一)——环境搭建
1.参考该教程装vue脚手架和创建工程 https://segmentfault.com/a/1190000008922234 附:在每个工程下cmd,执行npm install,此时工程下会多一个n ...
- Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/bin/tensorboard'
使用pip install --user tensorflow-serving-api命令即可
- RHEL6 学习:使用 cryptsetup 给分区加密
RHEL6 学习:使用 cryptsetup 给分区加密 今天学习了 RHEL 对硬盘分区加密的知识,在 RHEL 系统里可以通过使用 cryptsetup 工具对硬盘分区进行加密,加密后的分区需要输 ...
- 【Android-数据库Sqlite】Sqlite数据库 增、删、改、查
1.先创建一个Product类 Product.java 变量如下: int id; String code; String name; int qty; 2.创建一个DBHelper类 DBHelp ...