718. Maximum Length of Repeated Subarray

#week9

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

分析

   b  a  b

c  0  0  0

a  0  1  0

b  1  0  2

a  0  2  0

dp[i][j] = dp[i-1][j-1] + 1;

题解

 1 class Solution {
 2 public:
 3     int findLength(vector<int>& a, vector<int>& b) {
 4         int na = a.size(), nb= b.size();
 5         int dp[na+1][nb+1] = {};
 6         int mx = 0;
 7         for (int i = 1; i <= na; ++i) for (int j = 1; j <=nb; ++j) {
 8             if (a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
 9             mx = max(mx,dp[i][j]);
10         }
11
12         return mx;
13     }
14 };

原文地址:https://www.cnblogs.com/iamxiaoyubei/p/8278256.html

时间: 2024-10-08 04:59:57

718. Maximum Length of Repeated Subarray的相关文章

[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 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),

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

718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. LeetCode718. Maximum Length of Repeated Subarray 示例: 输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连

[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 <= len(A),

[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 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),

*Maximum Length Palindromic Sub-Sequence of an Array.

/* Write a function to compute the maximum length palindromic sub-sequence of an array. A palindrome is a sequence which is equal to its reverse. A sub-sequence of an array is a sequence which can be constructed by removing elements of the array. Ex:

Maximum length of a table name in MySQL

http://dev.mysql.com/doc/refman/5.7/en/identifiers.html The following table describes the maximum length for each type of identifier. Identifier Maximum Length (characters) Database 64 (NDB storage engine: 63) Table 64 (NDB storage engine: 63) Column

646. Maximum Length of Pair Chain

Problem statement 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 fashi

maximum sum of a subarray with at-least k elements.

// Returns maximum sum of a subarray with at-least // k elements. static int maxSumWithK(int a[], int n, int k) { // maxSum[i] is going to store maximum sum // till index i such that a[i] is part of the // sum. int maxSum[] = new int [n]; maxSum[0] =

Mysql:Changes in MySQL 5.7.13 (2016-06-02, General Availability):maximum length of MySQL user names was increased from 16 to 32 characters

Changes in MySQL 5.7.13 (2016-06-02, General Availability) Account Management Notes In MySQL 5.7.8, the maximum length of MySQL user names was increased from 16 to 32 characters, but some applicable contexts for this increase were overlooked. Additio