718. Maximum Length of Repeated Subarray
Input: nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output: 3
Explanation: The repeated subarray with maximum length is [3,2,1].Input: nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output: 5public class Solution {
public int findLength(List<Integer> A, List<Integer> B) {
int aSize = A.size();
int bSize = B.size();
int[][] dp = new int[aSize + 1][bSize + 1];
int result = 0;
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[i].length; j++) {
dp[i][j] = A.get(i - 1) == B.get(j - 1) ? dp[i - 1][j - 1] + 1 : 0;
result = Math.max(result, dp[i][j]);
}
}
return result;
}
}Last updated