Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 561fc9f

Browse files
feat: add solutions to lc problem: No.2657 (#1967)
No.2657.Find the Prefix Common Array of Two Arrays
1 parent 6e704f2 commit 561fc9f

File tree

7 files changed

+285
-72
lines changed

7 files changed

+285
-72
lines changed

‎solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README.md‎

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ i = 2:1,2 和 3 是两个数组的前缀公共元素,所以 C[2] = 3 。
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
**方法一:计数 + 枚举**
52+
**方法一:计数**
5353

5454
我们可以使用两个数组 $cnt1$ 和 $cnt2$ 分别记录数组 $A$ 和 $B$ 中每个元素出现的次数,用数组 $ans$ 记录答案。
5555

@@ -59,6 +59,18 @@ i = 2:1,2 和 3 是两个数组的前缀公共元素,所以 C[2] = 3 。
5959

6060
时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $A$ 和 $B$ 的长度。
6161

62+
**方法二:位运算(异或运算)**
63+
64+
我们可以使用一个长度为 $n+1$ 的数组 $vis$ 记录数组 $A$ 和 $B$ 中每个元素的出现情况,数组 $vis$ 的初始值为 1ドル$。另外,我们用一个变量 $s$ 记录当前公共元素的个数。
65+
66+
接下来,我们遍历数组 $A$ 和 $B,ドル更新 $vis[A[i]] = vis[A[i]] \oplus 1,ドル并且更新 $vis[B[i]] = vis[B[i]] \oplus 1,ドル其中 $\oplus$ 表示异或运算。
67+
68+
如果遍历到当前位置,元素 $A[i]$ 出现过两次(即在数组 $A$ 和 $B$ 中都出现过),那么 $vis[A[i]]$ 的值将为会 1ドル,ドル我们将 $s$ 加一。同理,如果元素 $B[i]$ 出现过两次,那么 $vis[B[i]]$ 的值将为会 1ドル,ドル我们将 $s$ 加一。然后将 $s$ 的值加入到答案数组 $ans$ 中。
69+
70+
遍历结束后,返回答案数组 $ans$ 即可。
71+
72+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是数组 $A$ 和 $B$ 的长度。
73+
6274
<!-- tabs:start -->
6375

6476
### **Python3**
@@ -79,6 +91,21 @@ class Solution:
7991
return ans
8092
```
8193

94+
```python
95+
class Solution:
96+
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
97+
ans = []
98+
vis = [1] * (len(A) + 1)
99+
s = 0
100+
for a, b in zip(A, B):
101+
vis[a] ^= 1
102+
s += vis[a]
103+
vis[b] ^= 1
104+
s += vis[b]
105+
ans.append(s)
106+
return ans
107+
```
108+
82109
### **Java**
83110

84111
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -102,6 +129,26 @@ class Solution {
102129
}
103130
```
104131

132+
```java
133+
class Solution {
134+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
135+
int n = A.length;
136+
int[] ans = new int[n];
137+
int[] vis = new int[n + 1];
138+
Arrays.fill(vis, 1);
139+
int s = 0;
140+
for (int i = 0; i < n; ++i) {
141+
vis[A[i]] ^= 1;
142+
s += vis[A[i]];
143+
vis[B[i]] ^= 1;
144+
s += vis[B[i]];
145+
ans[i] = s;
146+
}
147+
return ans;
148+
}
149+
}
150+
```
151+
105152
### **C++**
106153

107154
```cpp
@@ -123,6 +170,26 @@ public:
123170
};
124171
```
125172
173+
```cpp
174+
class Solution {
175+
public:
176+
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
177+
int n = A.size();
178+
vector<int> ans;
179+
vector<int> vis(n + 1, 1);
180+
int s = 0;
181+
for (int i = 0; i < n; ++i) {
182+
vis[A[i]] ^= 1;
183+
s += vis[A[i]];
184+
vis[B[i]] ^= 1;
185+
s += vis[B[i]];
186+
ans.push_back(s);
187+
}
188+
return ans;
189+
}
190+
};
191+
```
192+
126193
### **Go**
127194

128195
```go
@@ -143,14 +210,33 @@ func findThePrefixCommonArray(A []int, B []int) []int {
143210
}
144211
```
145212

213+
```go
214+
func findThePrefixCommonArray(A []int, B []int) (ans []int) {
215+
vis := make([]int, len(A)+1)
216+
for i := range vis {
217+
vis[i] = 1
218+
}
219+
s := 0
220+
for i, a := range A {
221+
b := B[i]
222+
vis[a] ^= 1
223+
s += vis[a]
224+
vis[b] ^= 1
225+
s += vis[b]
226+
ans = append(ans, s)
227+
}
228+
return
229+
}
230+
```
231+
146232
### **TypeScript**
147233

148234
```ts
149235
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
150236
const n = A.length;
151-
const cnt1: number[] = newArray(n + 1).fill(0);
152-
const cnt2: number[] = newArray(n + 1).fill(0);
153-
const ans: number[] = newArray(n).fill(0);
237+
const cnt1: number[] = Array(n + 1).fill(0);
238+
const cnt2: number[] = Array(n + 1).fill(0);
239+
const ans: number[] = Array(n).fill(0);
154240
for (let i = 0; i < n; ++i) {
155241
++cnt1[A[i]];
156242
++cnt2[B[i]];
@@ -162,6 +248,24 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {
162248
}
163249
```
164250

251+
```ts
252+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
253+
const n = A.length;
254+
const vis: number[] = Array(n + 1).fill(1);
255+
const ans: number[] = [];
256+
let s = 0;
257+
for (let i = 0; i < n; ++i) {
258+
const [a, b] = [A[i], B[i]];
259+
vis[a] ^= 1;
260+
s += vis[a];
261+
vis[b] ^= 1;
262+
s += vis[b];
263+
ans.push(s);
264+
}
265+
return ans;
266+
}
267+
```
268+
165269
### **...**
166270

167271
```

‎solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README_EN.md‎

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,27 @@ At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
4545

4646
## Solutions
4747

48-
**Solution 1: Count + Enumeration**
48+
**Solution 1: Counting**
4949

50-
We can use two arrays $cnt1$ and $cnt2$ to record the number of occurrences of each element in arrays $A$ and $B,ドル and use array $ans$ to record the answer.
50+
We can use two arrays $cnt1$ and $cnt2$ to record the occurrence times of each element in arrays $A$ and $B$ respectively, and use an array $ans$ to record the answer.
5151

52-
Traverse arrays $A$ and $B,ドル add the number of occurrences of $A[i]$ in $cnt1$ by one, and add the number of occurrences of $B[i]$ in $cnt2$ by one. Then enumerate $j \in [1,n],ドル calculate the minimum value of the number of occurrences of each element $j$ in $cnt1$ and $cnt2,ドル and add it to $ans[i]$.
52+
Traverse arrays $A$ and $B,ドル increment the occurrence times of $A[i]$ in $cnt1,ドル and increment the occurrence times of $B[i]$ in $cnt2$. Then enumerate $j \in [1,n],ドル calculate the minimum occurrence times of each element $j$ in $cnt1$ and $cnt2,ドル and accumulate them into $ans[i]$.
5353

54-
After the traversal is over, return the answer array $ans$.
54+
After the traversal, return the answer array $ans$.
5555

56-
The time complexity is $O(n^2),ドル and the space complexity is $O(n)$. Where $n$ is the length of arrays $A$ and $B$.
56+
The time complexity is $O(n^2),ドル and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$.
57+
58+
**Solution 2: Bit Operation (XOR Operation)**
59+
60+
We can use an array $vis$ of length $n+1$ to record the occurrence situation of each element in arrays $A$ and $B,ドル the initial value of array $vis$ is 1ドル$. In addition, we use a variable $s$ to record the current number of common elements.
61+
62+
Next, we traverse arrays $A$ and $B,ドル update $vis[A[i]] = vis[A[i]] \oplus 1,ドル and update $vis[B[i]] = vis[B[i]] \oplus 1,ドル where $\oplus$ represents XOR operation.
63+
64+
If at the current position, the element $A[i]$ has appeared twice (i.e., it has appeared in both arrays $A$ and $B$), then the value of $vis[A[i]]$ will be 1ドル,ドル and we increment $s$. Similarly, if the element $B[i]$ has appeared twice, then the value of $vis[B[i]]$ will be 1ドル,ドル and we increment $s$. Then add the value of $s$ to the answer array $ans$.
65+
66+
After the traversal, return the answer array $ans$.
67+
68+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$.
5769

5870
<!-- tabs:start -->
5971

@@ -73,6 +85,21 @@ class Solution:
7385
return ans
7486
```
7587

88+
```python
89+
class Solution:
90+
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
91+
ans = []
92+
vis = [1] * (len(A) + 1)
93+
s = 0
94+
for a, b in zip(A, B):
95+
vis[a] ^= 1
96+
s += vis[a]
97+
vis[b] ^= 1
98+
s += vis[b]
99+
ans.append(s)
100+
return ans
101+
```
102+
76103
### **Java**
77104

78105
```java
@@ -94,6 +121,26 @@ class Solution {
94121
}
95122
```
96123

124+
```java
125+
class Solution {
126+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
127+
int n = A.length;
128+
int[] ans = new int[n];
129+
int[] vis = new int[n + 1];
130+
Arrays.fill(vis, 1);
131+
int s = 0;
132+
for (int i = 0; i < n; ++i) {
133+
vis[A[i]] ^= 1;
134+
s += vis[A[i]];
135+
vis[B[i]] ^= 1;
136+
s += vis[B[i]];
137+
ans[i] = s;
138+
}
139+
return ans;
140+
}
141+
}
142+
```
143+
97144
### **C++**
98145

99146
```cpp
@@ -115,6 +162,26 @@ public:
115162
};
116163
```
117164
165+
```cpp
166+
class Solution {
167+
public:
168+
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
169+
int n = A.size();
170+
vector<int> ans;
171+
vector<int> vis(n + 1, 1);
172+
int s = 0;
173+
for (int i = 0; i < n; ++i) {
174+
vis[A[i]] ^= 1;
175+
s += vis[A[i]];
176+
vis[B[i]] ^= 1;
177+
s += vis[B[i]];
178+
ans.push_back(s);
179+
}
180+
return ans;
181+
}
182+
};
183+
```
184+
118185
### **Go**
119186

120187
```go
@@ -135,14 +202,33 @@ func findThePrefixCommonArray(A []int, B []int) []int {
135202
}
136203
```
137204

205+
```go
206+
func findThePrefixCommonArray(A []int, B []int) (ans []int) {
207+
vis := make([]int, len(A)+1)
208+
for i := range vis {
209+
vis[i] = 1
210+
}
211+
s := 0
212+
for i, a := range A {
213+
b := B[i]
214+
vis[a] ^= 1
215+
s += vis[a]
216+
vis[b] ^= 1
217+
s += vis[b]
218+
ans = append(ans, s)
219+
}
220+
return
221+
}
222+
```
223+
138224
### **TypeScript**
139225

140226
```ts
141227
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
142228
const n = A.length;
143-
const cnt1: number[] = newArray(n + 1).fill(0);
144-
const cnt2: number[] = newArray(n + 1).fill(0);
145-
const ans: number[] = newArray(n).fill(0);
229+
const cnt1: number[] = Array(n + 1).fill(0);
230+
const cnt2: number[] = Array(n + 1).fill(0);
231+
const ans: number[] = Array(n).fill(0);
146232
for (let i = 0; i < n; ++i) {
147233
++cnt1[A[i]];
148234
++cnt2[B[i]];
@@ -154,6 +240,24 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {
154240
}
155241
```
156242

243+
```ts
244+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
245+
const n = A.length;
246+
const vis: number[] = Array(n + 1).fill(1);
247+
const ans: number[] = [];
248+
let s = 0;
249+
for (let i = 0; i < n; ++i) {
250+
const [a, b] = [A[i], B[i]];
251+
vis[a] ^= 1;
252+
s += vis[a];
253+
vis[b] ^= 1;
254+
s += vis[b];
255+
ans.push(s);
256+
}
257+
return ans;
258+
}
259+
```
260+
157261
### **...**
158262

159263
```
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
class Solution {
2-
public:
3-
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
4-
int n = A.size();
5-
vector<int> ans(n);
6-
vector<int> cnt1(n + 1), cnt2(n + 1);
7-
for (int i = 0; i < n; ++i) {
8-
++cnt1[A[i]];
9-
++cnt2[B[i]];
10-
for (int j = 1; j <= n; ++j) {
11-
ans[i] += min(cnt1[j], cnt2[j]);
12-
}
13-
}
14-
return ans;
15-
}
1+
class Solution {
2+
public:
3+
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
4+
int n = A.size();
5+
vector<int> ans;
6+
vector<int> vis(n + 1, 1);
7+
int s = 0;
8+
for (int i = 0; i < n; ++i) {
9+
vis[A[i]] ^= 1;
10+
s += vis[A[i]];
11+
vis[B[i]] ^= 1;
12+
s += vis[B[i]];
13+
ans.push_back(s);
14+
}
15+
return ans;
16+
}
1617
};
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
func findThePrefixCommonArray(A []int, B []int) []int {
2-
n := len(A)
3-
cnt1 := make([]int, n+1)
4-
cnt2 := make([]int, n+1)
5-
ans := make([]int, n)
1+
func findThePrefixCommonArray(A []int, B []int) (ans []int) {
2+
vis := make([]int, len(A)+1)
3+
for i := range vis {
4+
vis[i] = 1
5+
}
6+
s := 0
67
for i, a := range A {
78
b := B[i]
8-
cnt1[a]++
9-
cnt2[b]++
10-
forj:= 1; j<=n; j++ {
11-
ans[i] += min(cnt1[j], cnt2[j])
12-
}
9+
vis[a]^=1
10+
s+=vis[a]
11+
vis[b] ^= 1
12+
s += vis[b]
13+
ans=append(ans, s)
1314
}
14-
returnans
15+
return
1516
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /