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 ...
随机推荐
- VMware虚拟机下实现Linux与window文件夹共享
这里说的是在VMware虚拟机下来实现在windows与Linux下共享一个文件夹. 下面来说明一下是如何实现的: 1.安装VMware.Workstation. 2.安装Fedora10. 3.完成 ...
- android日历控件
源码地址 : http://download.csdn.net/detail/abc13939746593/7265459
- MonoTouch.Dialog简介
新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...
- 记一次FastJSON和Jackson解析json时遇到的中括号问题
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/24395015 完整版见https://jadyer. ...
- HLS切片机
参考: 1,linux下搭建生成HLS所需的.ts和.m3u8文件http://www.cnblogs.com/mystory/archive/2013/04/07/3006200.html2,iPh ...
- Cocos2d-x如何添加新场景及切换新场景(包括场景特效)
做了一天多的工作终于把此功能搞定了,实际上添加新场景花费不了多少时间,时间主要花在切换到另一个场景的实现上,主要原因是编译时出现了一个错误,百思不得其解,后来经过查资料不断摸索才知道自己问题的所在,改 ...
- java之EJB
EjB,只是一个服务端运行组件,公开接口供客户端以C/S方式调用而已. 最直白,最本质的解释,可参见: http://blog.csdn.net/jojo52013145/article/detail ...
- powerbuilder
PowerBuilder美国Sybase公司研制的一种新型.快速开发工具,是客户机/服务器结构下,基于Windows3.x.Windows95和WindowsNT的一个集成化开发工具.它包含一个直观的 ...
- 使用jQuery集成Google翻译
利用jQuery,轻松将google翻译集成到你的web应用中. 1. [代码][JavaScript]代码 1<script src="Scripts/Translator ...
- JSTL取整、读取数组、字符串连接
以通过formatNumber去掉小数. <fmt:formatNumber type='number' value='${(tv.timeLong-tv.timeLong%60)/60 }' ...