-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path58-minimum-insertions-to-make-two-arrays-equal.cpp
62 lines (53 loc) · 1.23 KB
/
58-minimum-insertions-to-make-two-arrays-equal.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
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution {
public:
int LIS(vector<int> v)
{
vector<int> res;
if(v.size() > 0)
res.push_back(v[0]);
for(int i = 1; i < v.size(); ++i)
{
if(v[i] > res.back())
res.push_back(v[i]);
else
{
int id = lower_bound(res.begin(), res.end(), v[i]) - res.begin();
res[id] = v[i];
}
}
return res.size();
}
int minInsAndDel(int A[], int B[], int n, int m)
{
vector<int> tp;
for(int i = 0; i < n; ++i)
{
if(binary_search(B, B + m, A[i]))
{
tp.push_back(A[i]);
}
}
return (n + m - (2 * LIS(tp)));
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N,M;
cin>>N>>M;
int A[N], B[M];
for(int i=0; i<N; i++)
cin>>A[i];
for(int i=0; i<M; i++)
cin>>B[i];
Solution ob;
cout << ob.minInsAndDel(A,B,N,M) << endl;
}
return 0;
} // } Driver Code Ends