From 3126ce4615c72237422d8a037f710f7470bfcd15 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 8 Apr 2025 21:14:24 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2657 No.2657.Find the Prefix Common Array of Two Arrays --- .../README.md | 107 ++++++++++++++++++ .../README_EN.md | 107 ++++++++++++++++++ .../Solution3.cpp | 14 +++ .../Solution3.go | 11 ++ .../Solution3.java | 13 +++ .../Solution3.py | 9 ++ .../Solution3.ts | 21 ++++ 7 files changed, 282 insertions(+) create mode 100644 solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.cpp create mode 100644 solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.go create mode 100644 solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.java create mode 100644 solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.py create mode 100644 solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.ts diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README.md b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README.md index e0bfe58e23e11..113e96138ec9f 100644 --- a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README.md +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README.md @@ -300,4 +300,111 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] { + + +### 方法三:位运算(空间优化) + +由于题目中给定的数组 $A$ 和 $B$ 的元素范围是 $[1,n]$,且不超过 $50$,我们可以使用一个整数 $x$ 和一个整数 $y$ 来分别表示数组 $A$ 和 $B$ 中每个元素的出现情况。具体地,我们用整数 $x$ 的第 $i$ 位表示元素 $i$ 是否在数组 $A$ 中出现过,用整数 $y$ 的第 $i$ 位表示元素 $i$ 是否在数组 $B$ 中出现过。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $A$ 和 $B$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: + ans = [] + x = y = 0 + for a, b in zip(A, B): + x |= 1 << a + y |= 1 << b + ans.append((x & y).bit_count()) + return ans +``` + +#### Java + +```java +class Solution { + public int[] findThePrefixCommonArray(int[] A, int[] B) { + int n = A.length; + int[] ans = new int[n]; + long x = 0, y = 0; + for (int i = 0; i < n; i++) { + x |= 1L << A[i]; + y |= 1L << B[i]; + ans[i] = Long.bitCount(x & y); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector findThePrefixCommonArray(vector& A, vector& B) { + int n = A.size(); + vector ans(n); + long long x = 0, y = 0; + for (int i = 0; i < n; ++i) { + x |= (1LL << A[i]); + y |= (1LL << B[i]); + ans[i] = __builtin_popcountll(x & y); + } + return ans; + } +}; +``` + +#### Go + +```go +func findThePrefixCommonArray(A []int, B []int) []int { + n := len(A) + ans := make([]int, n) + var x, y int + for i := 0; i < n; i++ { + x |= 1 << A[i] + y |= 1 << B[i] + ans[i] = bits.OnesCount(uint(x & y)) + } + return ans +} +``` + +#### TypeScript + +```ts +function findThePrefixCommonArray(A: number[], B: number[]): number[] { + const n = A.length; + const ans: number[] = []; + let [x, y] = [0n, 0n]; + for (let i = 0; i < n; i++) { + x |= 1n << BigInt(A[i]); + y |= 1n << BigInt(B[i]); + ans.push(bitCount64(x & y)); + } + return ans; +} + +function bitCount64(i: bigint): number { + i = i - ((i >> 1n) & 0x5555555555555555n); + i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n); + i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn; + i = i + (i >> 8n); + i = i + (i >> 16n); + i = i + (i >> 32n); + return Number(i & 0x7fn); +} +``` + + + + + diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README_EN.md b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README_EN.md index 12976a4d9baad..7a97ac75babca 100644 --- a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README_EN.md +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/README_EN.md @@ -300,4 +300,111 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] { + + +### Solution 3: Bit Manipulation (Space Optimization) + +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$. + +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)$. + + + +#### Python3 + +```python +class Solution: + def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: + ans = [] + x = y = 0 + for a, b in zip(A, B): + x |= 1 << a + y |= 1 << b + ans.append((x & y).bit_count()) + return ans +``` + +#### Java + +```java +class Solution { + public int[] findThePrefixCommonArray(int[] A, int[] B) { + int n = A.length; + int[] ans = new int[n]; + long x = 0, y = 0; + for (int i = 0; i < n; i++) { + x |= 1L << A[i]; + y |= 1L << B[i]; + ans[i] = Long.bitCount(x & y); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector findThePrefixCommonArray(vector& A, vector& B) { + int n = A.size(); + vector ans(n); + long long x = 0, y = 0; + for (int i = 0; i < n; ++i) { + x |= (1LL << A[i]); + y |= (1LL << B[i]); + ans[i] = __builtin_popcountll(x & y); + } + return ans; + } +}; +``` + +#### Go + +```go +func findThePrefixCommonArray(A []int, B []int) []int { + n := len(A) + ans := make([]int, n) + var x, y int + for i := 0; i < n; i++ { + x |= 1 << A[i] + y |= 1 << B[i] + ans[i] = bits.OnesCount(uint(x & y)) + } + return ans +} +``` + +#### TypeScript + +```ts +function findThePrefixCommonArray(A: number[], B: number[]): number[] { + const n = A.length; + const ans: number[] = []; + let [x, y] = [0n, 0n]; + for (let i = 0; i < n; i++) { + x |= 1n << BigInt(A[i]); + y |= 1n << BigInt(B[i]); + ans.push(bitCount64(x & y)); + } + return ans; +} + +function bitCount64(i: bigint): number { + i = i - ((i >> 1n) & 0x5555555555555555n); + i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n); + i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn; + i = i + (i >> 8n); + i = i + (i >> 16n); + i = i + (i >> 32n); + return Number(i & 0x7fn); +} +``` + + + + + diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.cpp b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.cpp new file mode 100644 index 0000000000000..2d757723802d7 --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + vector findThePrefixCommonArray(vector& A, vector& B) { + int n = A.size(); + vector ans(n); + long long x = 0, y = 0; + for (int i = 0; i < n; ++i) { + x |= (1LL << A[i]); + y |= (1LL << B[i]); + ans[i] = __builtin_popcountll(x & y); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.go b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.go new file mode 100644 index 0000000000000..115e0dfad48fb --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.go @@ -0,0 +1,11 @@ +func findThePrefixCommonArray(A []int, B []int) []int { + n := len(A) + ans := make([]int, n) + var x, y int + for i := 0; i < n; i++ { + x |= 1 << A[i] + y |= 1 << B[i] + ans[i] = bits.OnesCount(uint(x & y)) + } + return ans +} \ No newline at end of file diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.java b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.java new file mode 100644 index 0000000000000..1c4903e9a84ef --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.java @@ -0,0 +1,13 @@ +class Solution { + public int[] findThePrefixCommonArray(int[] A, int[] B) { + int n = A.length; + int[] ans = new int[n]; + long x = 0, y = 0; + for (int i = 0; i < n; i++) { + x |= 1L << A[i]; + y |= 1L << B[i]; + ans[i] = Long.bitCount(x & y); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.py b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.py new file mode 100644 index 0000000000000..20c97b183bf20 --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.py @@ -0,0 +1,9 @@ +class Solution: + def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: + ans = [] + x = y = 0 + for a, b in zip(A, B): + x |= 1 << a + y |= 1 << b + ans.append((x & y).bit_count()) + return ans diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.ts b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.ts new file mode 100644 index 0000000000000..d828c35d32c4c --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.ts @@ -0,0 +1,21 @@ +function findThePrefixCommonArray(A: number[], B: number[]): number[] { + const n = A.length; + const ans: number[] = []; + let [x, y] = [0n, 0n]; + for (let i = 0; i < n; i++) { + x |= 1n << BigInt(A[i]); + y |= 1n << BigInt(B[i]); + ans.push(bitCount64(x & y)); + } + return ans; +} + +function bitCount64(i: bigint): number { + i = i - ((i >> 1n) & 0x5555555555555555n); + i = (i & 0x3333333333333333n) + ((i >> 2n) & 0x3333333333333333n); + i = (i + (i >> 4n)) & 0x0f0f0f0f0f0f0f0fn; + i = i + (i >> 8n); + i = i + (i >> 16n); + i = i + (i >> 32n); + return Number(i & 0x7fn); +}