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. 1 <= len(A), len(B) <= 1000
  2. 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的更多相关文章

  1. 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 ...

  2. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  3. 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 ...

  4. [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 ...

  5. LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

    718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...

  6. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 【BZOJ3193】[JLOI2013]地形生成 DP

    [BZOJ3193][JLOI2013]地形生成 Description 最近IK正在做关于地形建模的工作.其中一个工作阶段就是把一些山排列成一行.每座山都有各不相同的标号和高度.为了遵从一些设计上的 ...

  2. python2&python3的区别

    区别1. python3中>>>range<3,6>range<3,6> python2中>>>range<3,6>[3,4,5 ...

  3. 什么是 AQS ?

    1.什么是AQS? AQS是英文单词AbstractQueuedSynchronizer的缩写,翻译过来就是队列同步器. 它是构建锁或者其他同步组件的基础框架(如ReentrantLock.Reent ...

  4. ElasticSearch(七)容错机制

    一.关于横向扩容 PUT /test_index { "settings" : { "number_of_shards" : 3, "number_o ...

  5. node.js 开发博客系统

    1. 安装yoman :npm install -g yo 2. 安装 generator-express :npm install -g generator-express 3. 安装 bower ...

  6. HTML5 Canvas 时钟

    1. [图片] QQ截图20120712130049.png ​2. [代码][HTML]代码 <!DOCTYPE html><html lang="en" &g ...

  7. PIL数据和numpy数据的相互转换

    在做图像处理的时候,自己常用的是将PIL的图片对象转换成为numpy的数组,同时也将numpy中的数组转换成为对应的图片对象. 这里考虑使用PIL来进行图像的一般处理. from PIL import ...

  8. Asterisk 通话过程中执行动作(即applicationmap )的使用方法和电话转会议的实现

      asterisk在正常通话过程中执行拨号计划中动作是通过feature.conf中的[applicationmap ]下定义的,举例如下: nway-start => *0,callee,M ...

  9. 基于WinDbg的内存泄漏分析

    在前面C++中基于Crt的内存泄漏检测一文中提到的方法已经可以解决我们的大部分内存泄露问题了,但是该方法是有前提的,那就是一定要有源代码,而且还只能是Debug版本调试模式下.实际上很多时候我们的程序 ...

  10. 是否要从单片机转为嵌入式Linux?

    作者:嵌入式老鸟火哥 授权转载于公众号嵌入式老鸟的职场之道(ID: ict_embedded),有增加内容和修改. 最近很多童鞋投票并咨询如何从单片机转为嵌入式Linux开发.看来读者圈中做单片机,R ...