[LeetCode] Maximum Length of Repeated Subarray

718. Maximum Length of Repeated Subarray

Given two integer arrays nums1 and nums2, return the maximum length of a subarray that appears in both arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
int res(0), n(nums1.size()), m(nums2.size());
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(nums1[i] == nums2[j]) res = max(res, dp[i + 1][j + 1] = dp[i][j] + 1);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/23/PS/LeetCode/maximum-length-of-repeated-subarray/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.