From d87ab44d151150506c8311913da25c50ec673302 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 14 Jan 2025 15:43:36 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.2657 --- .../README.md | 56 +++++++++++++++++++ .../README_EN.md | 56 +++++++++++++++++++ .../Solution3.js | 17 ++++++ .../Solution3.ts | 17 ++++++ 4 files changed, 146 insertions(+) create mode 100644 solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.js 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..a639f233813c8 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,60 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] { + + +### Solution 3: Counting + Set + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$. + + + +#### TypeScript + +```ts +function findThePrefixCommonArray(A: number[], B: number[]): number[] { + const n = A.length; + const res = Array(n).fill(0); + const [setA, setB] = [new Set(), new Set()]; + + for (let i = 0, c = 0; i < n; i++) { + if (setA.has(B[i])) c++; + if (setB.has(A[i])) c++; + if (A[i] === B[i]) c++; + + setA.add(A[i]); + setB.add(B[i]); + res[i] = c; + } + + return res; +} +``` + +#### JavaScript + +```js +function findThePrefixCommonArray(A, B) { + const n = A.length; + const res = Array(n).fill(0); + const [setA, setB] = [new Set(), new Set()]; + + for (let i = 0, c = 0; i < n; i++) { + if (setA.has(B[i])) c++; + if (setB.has(A[i])) c++; + if (A[i] === B[i]) c++; + + setA.add(A[i]); + setB.add(B[i]); + res[i] = c; + } + + return res; +} +``` + + + + + 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..671be6f6f7ad6 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,60 @@ function findThePrefixCommonArray(A: number[], B: number[]): number[] { + + +### Solution 3: Counting + Set + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of arrays $A$ and $B$. + + + +#### TypeScript + +```ts +function findThePrefixCommonArray(A: number[], B: number[]): number[] { + const n = A.length; + const res = Array(n).fill(0); + const [setA, setB] = [new Set(), new Set()]; + + for (let i = 0, c = 0; i < n; i++) { + if (setA.has(B[i])) c++; + if (setB.has(A[i])) c++; + if (A[i] === B[i]) c++; + + setA.add(A[i]); + setB.add(B[i]); + res[i] = c; + } + + return res; +} +``` + +#### JavaScript + +```js +function findThePrefixCommonArray(A, B) { + const n = A.length; + const res = Array(n).fill(0); + const [setA, setB] = [new Set(), new Set()]; + + for (let i = 0, c = 0; i < n; i++) { + if (setA.has(B[i])) c++; + if (setB.has(A[i])) c++; + if (A[i] === B[i]) c++; + + setA.add(A[i]); + setB.add(B[i]); + res[i] = c; + } + + return res; +} +``` + + + + + diff --git a/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.js b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.js new file mode 100644 index 0000000000000..d8cf56614c4c5 --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.js @@ -0,0 +1,17 @@ +function findThePrefixCommonArray(A, B) { + const n = A.length; + const res = Array(n).fill(0); + const [setA, setB] = [new Set(), new Set()]; + + for (let i = 0, c = 0; i < n; i++) { + if (setA.has(B[i])) c++; + if (setB.has(A[i])) c++; + if (A[i] === B[i]) c++; + + setA.add(A[i]); + setB.add(B[i]); + res[i] = c; + } + + return res; +} 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..062f87b06adb6 --- /dev/null +++ b/solution/2600-2699/2657.Find the Prefix Common Array of Two Arrays/Solution3.ts @@ -0,0 +1,17 @@ +function findThePrefixCommonArray(A: number[], B: number[]): number[] { + const n = A.length; + const res = Array(n).fill(0); + const [setA, setB] = [new Set(), new Set()]; + + for (let i = 0, c = 0; i < n; i++) { + if (setA.has(B[i])) c++; + if (setB.has(A[i])) c++; + if (A[i] === B[i]) c++; + + setA.add(A[i]); + setB.add(B[i]); + res[i] = c; + } + + return res; +}