Skip to content

Commit 01b8a10

Browse files
authored
feat: add solutions to lc problem: No.2657 (#4338)
No.2657.Find the Prefix Common Array of Two Arrays close #3952
1 parent 8ab1414 commit 01b8a10

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed

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

+107
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,111 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {
300300

301301
<!-- solution:end -->
302302

303+
<!-- solution:start -->
304+
305+
### 方法三:位运算(空间优化)
306+
307+
由于题目中给定的数组 $A$ 和 $B$ 的元素范围是 $[1,n]$,且不超过 $50$,我们可以使用一个整数 $x$ 和一个整数 $y$ 来分别表示数组 $A$ 和 $B$ 中每个元素的出现情况。具体地,我们用整数 $x$ 的第 $i$ 位表示元素 $i$ 是否在数组 $A$ 中出现过,用整数 $y$ 的第 $i$ 位表示元素 $i$ 是否在数组 $B$ 中出现过。
308+
309+
时间复杂度 $O(n)$,其中 $n$ 是数组 $A$ 和 $B$ 的长度。空间复杂度 $O(1)$。
310+
311+
<!-- tabs:start -->
312+
313+
#### Python3
314+
315+
```python
316+
class Solution:
317+
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
318+
ans = []
319+
x = y = 0
320+
for a, b in zip(A, B):
321+
x |= 1 << a
322+
y |= 1 << b
323+
ans.append((x & y).bit_count())
324+
return ans
325+
```
326+
327+
#### Java
328+
329+
```java
330+
class Solution {
331+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
332+
int n = A.length;
333+
int[] ans = new int[n];
334+
long x = 0, y = 0;
335+
for (int i = 0; i < n; i++) {
336+
x |= 1L << A[i];
337+
y |= 1L << B[i];
338+
ans[i] = Long.bitCount(x & y);
339+
}
340+
return ans;
341+
}
342+
}
343+
```
344+
345+
#### C++
346+
347+
```cpp
348+
class Solution {
349+
public:
350+
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
351+
int n = A.size();
352+
vector<int> ans(n);
353+
long long x = 0, y = 0;
354+
for (int i = 0; i < n; ++i) {
355+
x |= (1LL << A[i]);
356+
y |= (1LL << B[i]);
357+
ans[i] = __builtin_popcountll(x & y);
358+
}
359+
return ans;
360+
}
361+
};
362+
```
363+
364+
#### Go
365+
366+
```go
367+
func findThePrefixCommonArray(A []int, B []int) []int {
368+
n := len(A)
369+
ans := make([]int, n)
370+
var x, y int
371+
for i := 0; i < n; i++ {
372+
x |= 1 << A[i]
373+
y |= 1 << B[i]
374+
ans[i] = bits.OnesCount(uint(x & y))
375+
}
376+
return ans
377+
}
378+
```
379+
380+
#### TypeScript
381+
382+
```ts
383+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
384+
const n = A.length;
385+
const ans: number[] = [];
386+
let [x, y] = [0n, 0n];
387+
for (let i = 0; i < n; i++) {
388+
x |= 1n << BigInt(A[i]);
389+
y |= 1n << BigInt(B[i]);
390+
ans.push(bitCount64(x & y));
391+
}
392+
return ans;
393+
}
394+
395+
function bitCount64(i: bigint): number {
396+
i = i - ((i >> 1n) & 0x5555555555555555n);
397+
i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n);
398+
i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn;
399+
i = i + (i >> 8n);
400+
i = i + (i >> 16n);
401+
i = i + (i >> 32n);
402+
return Number(i & 0x7fn);
403+
}
404+
```
405+
406+
<!-- tabs:end -->
407+
408+
<!-- solution:end -->
409+
303410
<!-- problem:end -->

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

+107
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,111 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] {
300300

301301
<!-- solution:end -->
302302

303+
<!-- solution:start -->
304+
305+
### Solution 3: Bit Manipulation (Space Optimization)
306+
307+
Since the elements of arrays $A$ and $B$ are in the range $[1, n]$ and do not exceed $50$, we can use an integer $x$ and an integer $y$ to represent the occurrence of each element in arrays $A$ and $B$, respectively. Specifically, we use the $i$-th bit of integer $x$ to indicate whether element $i$ has appeared in array $A$, and the $i$-th bit of integer $y$ to indicate whether element $i$ has appeared in array $B$.
308+
309+
The time complexity of this solution is $O(n)$, where $n$ is the length of arrays $A$ and $B$. The space complexity is $O(1)$.
310+
311+
<!-- tabs:start -->
312+
313+
#### Python3
314+
315+
```python
316+
class Solution:
317+
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
318+
ans = []
319+
x = y = 0
320+
for a, b in zip(A, B):
321+
x |= 1 << a
322+
y |= 1 << b
323+
ans.append((x & y).bit_count())
324+
return ans
325+
```
326+
327+
#### Java
328+
329+
```java
330+
class Solution {
331+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
332+
int n = A.length;
333+
int[] ans = new int[n];
334+
long x = 0, y = 0;
335+
for (int i = 0; i < n; i++) {
336+
x |= 1L << A[i];
337+
y |= 1L << B[i];
338+
ans[i] = Long.bitCount(x & y);
339+
}
340+
return ans;
341+
}
342+
}
343+
```
344+
345+
#### C++
346+
347+
```cpp
348+
class Solution {
349+
public:
350+
vector<int> findThePrefixCommonArray(vector<int>& A, vector<int>& B) {
351+
int n = A.size();
352+
vector<int> ans(n);
353+
long long x = 0, y = 0;
354+
for (int i = 0; i < n; ++i) {
355+
x |= (1LL << A[i]);
356+
y |= (1LL << B[i]);
357+
ans[i] = __builtin_popcountll(x & y);
358+
}
359+
return ans;
360+
}
361+
};
362+
```
363+
364+
#### Go
365+
366+
```go
367+
func findThePrefixCommonArray(A []int, B []int) []int {
368+
n := len(A)
369+
ans := make([]int, n)
370+
var x, y int
371+
for i := 0; i < n; i++ {
372+
x |= 1 << A[i]
373+
y |= 1 << B[i]
374+
ans[i] = bits.OnesCount(uint(x & y))
375+
}
376+
return ans
377+
}
378+
```
379+
380+
#### TypeScript
381+
382+
```ts
383+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
384+
const n = A.length;
385+
const ans: number[] = [];
386+
let [x, y] = [0n, 0n];
387+
for (let i = 0; i < n; i++) {
388+
x |= 1n << BigInt(A[i]);
389+
y |= 1n << BigInt(B[i]);
390+
ans.push(bitCount64(x & y));
391+
}
392+
return ans;
393+
}
394+
395+
function bitCount64(i: bigint): number {
396+
i = i - ((i >> 1n) & 0x5555555555555555n);
397+
i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n);
398+
i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn;
399+
i = i + (i >> 8n);
400+
i = i + (i >> 16n);
401+
i = i + (i >> 32n);
402+
return Number(i & 0x7fn);
403+
}
404+
```
405+
406+
<!-- tabs:end -->
407+
408+
<!-- solution:end -->
409+
303410
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
long long x = 0, y = 0;
7+
for (int i = 0; i < n; ++i) {
8+
x |= (1LL << A[i]);
9+
y |= (1LL << B[i]);
10+
ans[i] = __builtin_popcountll(x & y);
11+
}
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func findThePrefixCommonArray(A []int, B []int) []int {
2+
n := len(A)
3+
ans := make([]int, n)
4+
var x, y int
5+
for i := 0; i < n; i++ {
6+
x |= 1 << A[i]
7+
y |= 1 << B[i]
8+
ans[i] = bits.OnesCount(uint(x & y))
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
3+
int n = A.length;
4+
int[] ans = new int[n];
5+
long x = 0, y = 0;
6+
for (int i = 0; i < n; i++) {
7+
x |= 1L << A[i];
8+
y |= 1L << B[i];
9+
ans[i] = Long.bitCount(x & y);
10+
}
11+
return ans;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
3+
ans = []
4+
x = y = 0
5+
for a, b in zip(A, B):
6+
x |= 1 << a
7+
y |= 1 << b
8+
ans.append((x & y).bit_count())
9+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function findThePrefixCommonArray(A: number[], B: number[]): number[] {
2+
const n = A.length;
3+
const ans: number[] = [];
4+
let [x, y] = [0n, 0n];
5+
for (let i = 0; i < n; i++) {
6+
x |= 1n << BigInt(A[i]);
7+
y |= 1n << BigInt(B[i]);
8+
ans.push(bitCount64(x & y));
9+
}
10+
return ans;
11+
}
12+
13+
function bitCount64(i: bigint): number {
14+
i = i - ((i >> 1n) & 0x5555555555555555n);
15+
i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n);
16+
i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn;
17+
i = i + (i >> 8n);
18+
i = i + (i >> 16n);
19+
i = i + (i >> 32n);
20+
return Number(i & 0x7fn);
21+
}

0 commit comments

Comments
 (0)