find-the-prefix-common-array-of-two-arrays
1.0.0
Find the Prefix Common Array of Two Arrays
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
5
class
Solution
6
{
7
public
:
8
vector<int>
findThePrefixCommonArray
(vector<int>& A, vector<int>& B)
9
{
10
set<int> seen;
11
int
n = (int)A.size();
12
vector<int> prefix_common(n, 0);
13
for
(
int
i = 0; i < n; ++i)
14
{
15
if
(!seen.empty() && seen.find(A[i]) != seen.end())
16
prefix_common[i]++;
17
else
18
seen.insert(A[i]);
19
20
if
(!seen.empty() && seen.find(B[i]) != seen.end())
21
prefix_common[i]++;
22
else
23
seen.insert(B[i]);
24
25
if
(i != 0)
26
prefix_common[i] += prefix_common[i - 1];
27
}
28
return
prefix_common;
29
}
30
};
31
32
int
main
()
33
{
34
vector<int> A = {1,3,2,4};
35
vector<int> B = {3,1,2,4};
36
vector<int> output =
Solution
().
findThePrefixCommonArray
(A, B);
37
for
(
int
el: output)
38
cout << el <<
" "
;
39
cout << endl;
40
return
0;
41
}
Solution
Definition
main.cpp:6
Solution::findThePrefixCommonArray
vector< int > findThePrefixCommonArray(vector< int > &A, vector< int > &B)
Definition
main.cpp:8
main
int main()
Definition
main.cpp:32
main.cpp
Generated by
1.9.8