-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMaximum Length of Repeated Subarray.cpp
58 lines (47 loc) · 1.21 KB
/
Maximum Length of Repeated Subarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
public:
int findLength(vector<int>& nums1, vector<int>& nums2) {
int n1 = nums1.size();
int n2 = nums2.size();
//moving num2 on num1
int ptr2 = 0;
int cnt = 0;
int largest = INT_MIN;
for(int i=0;i<n1;i++)
{
cnt = 0;
for(int j=i,ptr2=0;j<n1 && ptr2<n2;j++,ptr2++)
{
if(nums1[j]==nums2[ptr2])
{
cnt++;
}
else
{
cnt = 0;
}
largest = max(largest,cnt);
}
}
//moving num1 on num2
ptr2 = 0;
cnt = 0;
for(int i=0;i<n2;i++)
{
cnt = 0;
for(int j=i,ptr2=0;j<n2 && ptr2<n1;j++,ptr2++)
{
if(nums2[j]==nums1[ptr2])
{
cnt++;
}
else
{
cnt = 0;
}
largest = max(largest,cnt);
}
}
return largest;
}
};