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
Approach #1: Dynamic programming
class Solution {
public:
int findLength(vector<int>& A, vector<int>& B) {
int ans = 0;
int lenA = A.size();
int lenB = B.size();
vector<vector<int>> memo(lenA+1, vector<int>(lenB+1, 0));
for (int i = lenA-1; i >= 0; --i) {
for (int j = lenB-1; j >= 0; --j) {
if (A[i] == B[j]) {
memo[i][j] = memo[i+1][j+1] + 1;
ans = max(ans, memo[i][j]);
}
}
}
return ans;
}
};
Runtime: 92 ms, faster than 65.15% of C++ online submissions for Maximum Length of Repeated Subarray.
Analysis:
maybe using dynamic programming from back to front is the key to solve these similar questions.
there are some other ways to solve this problem.
https://leetcode.com/problems/maximum-length-of-repeated-subarray/
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 ...
- 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 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 ...
随机推荐
- 使用Apache Commons Chain(转载)
原博客出处:http://phil-xzh.iteye.com/blog/321536 使用Commons Chain 作为程序开发人员,我们经常需要对一个实际上程序性的系统应用面向对象的方法.商业分 ...
- IGP和EGP(转载)
AS(自治系统) - 也称为路由域,是指一个共同管理区域内的一组路由器.例如公司的内部网络和 Internet 服务提供商的网络.由于 Internet 基于自治系统,因此既需要使用内部路由协议,也需 ...
- EasyPlayer开源流媒体移动端播放器推出RTSP-RTMP-HTTP-HLS全功能Pro版
EasyPlayerPro介绍 Android EasyPlayerPro专业版全功能播放器,是由EasyDarwin开源团队维护的一款支持RTSP.RTMP.HTTP.HLS多种流媒体协议的播放器版 ...
- iOS不用上架就能下载安装ipa应用内测:使用FIR.im发布自己的移动端APP
本文转自:http://www.cnblogs.com/imzzk/p/firim.html 一次很偶然的机会知道fir.im,这家公司主要的产品就是帮助开发者方便便捷地发布iOS或者Android应 ...
- js怎么限制文本框input只能输入数字
1.说明 本篇文章介绍怎么使用js限制文本框只能输入数字 2.HTML代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1 ...
- gitlab 外网 无法访问 查端口 看文档
云服务器安装成功后 curl 页面可以正常跳转 重置密码的token 页面可以生成 但是 外网无法 访问 [root@test ~]# curl 127.0.0.1:18021 <htm ...
- 简单监控网站访问是否正常的shell脚本,邮件报警。网站恢复后继续运行。
#!/bin/bash # 使用curl检查网页是否可以正常访问,如果无法访问则发邮件. SITE=crm.bjzgjh.com PROT=80 URL="http://$SITE:$PRO ...
- machine learning for hacker记录(2) 数据分析
本章主要讲了对数据的一些基本探索,常见的six numbers,方差,均值等 > data.file <- file.path('data', '01_heights_weights_ge ...
- 测试覆盖率Emma工具使用
Emma使用与分析 #什么是Emma EMMA 是一个开源.面向 Java 程序测试覆盖率收集和报告工具.它通过对编译后的 Java 字节码文件进行插装,在测试执行过程中收集覆盖率信息,并通过支持多种 ...
- Vue中的methods、watch、computed
看到这个标题就知道这篇文章接下来要讲的内容,我们在使用vue的时候methods.watch.computed这三个特性一定经常使用,因为它们是非常的有用,但是没有彻底的理解它们的区别和各自的使用场景 ...