Given two integer arrays nums1
and nums2
, return the maximum length of a subarray that appears in both arrays.
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: 5 Explanation: The repeated subarray with maximum length is [0,0,0,0,0].
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 100
impl Solution {
pub fn find_length(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let mut dp = vec![vec![0; nums2.len() + 1]; nums1.len() + 1];
let mut ret = 0;
for i in 0..nums1.len() {
for j in 0..nums2.len() {
if nums1[i] == nums2[j] {
dp[i + 1][j + 1] = dp[i][j] + 1;
ret = ret.max(dp[i + 1][j + 1]);
}
}
}
ret
}
}