Skip to content

Commit 420ace8

Browse files
committed
Add solution #2657
1 parent 70b6181 commit 420ace8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@
431431
2648|[Generate Fibonacci Sequence](./2648-generate-fibonacci-sequence.js)|Easy|
432432
2649|[Nested Array Generator](./2649-nested-array-generator.js)|Medium|
433433
2650|[Design Cancellable Function](./2650-design-cancellable-function.js)|Hard|
434+
2657|[Find the Prefix Common Array of Two Arrays](./2657-find-the-prefix-common-array-of-two-arrays.js)|Medium|
434435
2665|[Counter II](./2665-counter-ii.js)|Easy|
435436
2666|[Allow One Function Call](./2666-allow-one-function-call.js)|Easy|
436437
2667|[Create Hello World Function](./2667-create-hello-world-function.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2657. Find the Prefix Common Array of Two Arrays
3+
* https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/
4+
* Difficulty: Medium
5+
*
6+
* You are given two 0-indexed integer permutations A and B of length n.
7+
*
8+
* A prefix common array of A and B is an array C such that C[i] is equal to the count of
9+
* numbers that are present at or before the index i in both A and B.
10+
*
11+
* Return the prefix common array of A and B.
12+
*
13+
* A sequence of n integers is called a permutation if it contains all integers from 1 to
14+
* n exactly once.
15+
*/
16+
17+
/**
18+
* @param {number[]} A
19+
* @param {number[]} B
20+
* @return {number[]}
21+
*/
22+
var findThePrefixCommonArray = function(A, B) {
23+
const result = [];
24+
25+
for (let i = 0, count = 0, set = new Set(); i < A.length; i++) {
26+
if (set.has(A[i])) count++;
27+
if (set.has(B[i])) count++;
28+
if (A[i] === B[i]) count++;
29+
[A[i], B[i]].forEach(n => set.add(n));
30+
result.push(count);
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)