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. android mvp高速开发框架介绍(dileber的简单介绍)

    今天我为大家介绍一款android mvp框架:dileber(https://github.com/dileber/dileber.git) 官方交流qq群:171443726 我个人qq:2971 ...

  2. easyui datagrid 加载静态文件中的json数据

    本文主要介绍easyui datagrid 怎么加载静态文件里的json数据,开发环境vs2012, 一.json文件所处的位置 二.json文件内容 {"total":28,&q ...

  3. 九度OJ 1118:数制转换 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3873 解决:1494 题目描述: 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所能表达的范围之内.     不 ...

  4. POJO对象建立规则

    1.所有POJO类属性必须使用包装数据类型,RPC方法的返回值和参数必须使用包装数据类型. 说明:POJO类属性没有初值是提醒使用者在使用时,必须自己显示的进行赋值,任何NPE问题,或者入库检查,都由 ...

  5. SpringBoot-(7)-基于Web,JDBC,MySql,Druid,MyBatis整合创建SpringBoot项目

    1 打开Spring Initializr 如果Project SDK为空,手动指定下JDK SDK路径 2, 填写Group和Artifact 3,选择依赖 选择Web,MySQL,JDBC,MyB ...

  6. Interpreter Pattern

    1.Interpreter模式的目的就是提供一个一门定义语言的语法表示的解释器,然后通过这个解释器来解释语言中的句子. 2.Interpreter模式结构图 3.实现 #ifndef _CONTEXT ...

  7. linux iptables:安全应用,防火墙

    iptables:安全应用,防火墙 windows和linux都有防火墙,企业的边缘会部署防火墙保证企业内部的局域网是安全的.针对个人电脑会有防火墙保证系统是安全的. 防火墙是唯一通道. 防火墙分类( ...

  8. LightOJ - 1030 Discovering Gold —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold    PDF (English) Statistics For ...

  9. 人生苦短之Python多线程

    #encoding=utf-8 import threading import time ''' python多线程并不是真正意义上的多线程,通常我们所说的多线程是多个线程同时执行某功能,而在pyth ...

  10. Nginx配置故障转移

    当上游服务器(真实访问服务器),一旦出现故障或者是没有及时相应的话,应该直接轮训到下一台服务器,保证服务器的高可用. 如果上游服务器的某一台宕机了,直接轮训到下一个~ 8080 8081 8082 关 ...