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
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].
Solution
class Solution:
def findLength(self, A: List[int], B: List[int]) -> int:
len1, len2 = len(A), len(B)
dp = [[0]*(len2+1) for _ in range(len1+1)]
for i in range(len1):
for j in range(len2):
if A[i] == B[j]:
dp[i+1][j+1] = dp[i][j]+1
return max(max(row) for row in dp)