[LeetCode] 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. 1 <= len(A), len(B) <= 1000
    2. 0 <= A[i], B[i] < 100

 

这道题给了我们两个数组A和B,让我们返回连个数组的最长重复子数组。那么如果我们将数组换成字符串,实际这道题就是求Longest Common Substring的问题了,而貌似LeetCode上并没有这种明显的要求最长相同子串的题,注意需要跟最长子序列Longest Common Subsequence区分开,关于最长子序列会在follow up中讨论。好,先来看这道题,对于这种求极值的问题,DP是不二之选,我们使用一个二维的DP数组,其中dp[i][j]表示数组A的前i个数字和数组B的前j个数字的最长子数组的长度,如果dp[i][j]不为0,则A中第i个数组和B中第j个数字必须相等,比对于这两个数组[1,2,2]和[3,1,2],我们的dp数组为: 

  3 1 2
1 0 1 0
2 0 0 2
2 0 0 1

我们注意观察,dp值不为0的地方,都是当A[i] == B[j]的地方,而且还要加上左上方的dp值,即dp[i-1][j-1],所以当前的dp[i][j]就等于dp[i-1][j-1] + 1,而一旦A[i] != B[j]时,直接赋值为0,不用多想,因为子数组是要连续的,一旦不匹配了,就不能再增加长度了。我们每次算出一个dp值,都要用来更新结果res,这样就能得到最长相同子数组的长度了,参见代码如下:

class Solution {
public:
    int findLength(vector<int>& A, vector<int>& B) {
        int res = 0;
        vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1, 0));
        for (int i = 1; i < dp.size(); ++i) {
            for (int j = 1; j < dp[i].size(); ++j) {
                dp[i][j] = (A[i - 1] == B[j - 1]) ? dp[i - 1][j - 1] + 1 : 0;
                res = max(res, dp[i][j]);
            }
        }
        return res;
    }
};

Follow up:在开始时,博主提到了要跟最长相同子序列Longest Common Subsequence区分开来,虽然LeetCode没有直接求最大相同子序列的题,但有几道题利用到了求该问题的思想,比如Delete Operation for Two StringsMinimum ASCII Delete Sum for Two Strings等,详细讨论请参见评论区一楼 :)

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

,如需转载请自行联系原博主。

时间: 2024-10-04 01:51:51

[LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组的相关文章

[LeetCode] Maximum Length of Pair Chain 链对的最大长度

You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. Given a set of

[LeetCode] Maximum Average Subarray II 子数组的最大平均值之二

Given an array consisting of n integers, find the contiguous subarray whose length is greater than or equal to k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output:

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

[LeetCode] Maximum Binary Tree 最大二叉树

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number

[LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level

[LeetCode] Maximum Swap 最大置换

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Inp

[LeetCode] Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 常规思路 class Solution { public: int maxProd

[LeetCode]58.Length of Last Word

题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

LeetCode 14 Longest Common Prefix(最长公共前缀)

翻译 写一个函数(或方法)来寻找一个字符串数组中的最长公共前缀. 原文 Write a function to find the longest common prefix string amongst an array of strings. 释义 "abcdefg" "abcdefghijk" "abcdfghijk" "abcef" 上面的字符串数组的最长公共前缀就是"abc". 思考 如下图所示,第