-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 2657
- Loading branch information
Showing
3 changed files
with
68 additions
and
12 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# [2657.Find the Prefix Common Array of Two Arrays][title] | ||
|
||
## Description | ||
You are given two **0-indexed** integer permutations `A` and `B` of length `n`. | ||
|
||
A **prefix common array** of `A` and `B` is an array `C` such that `C[i]` is equal to the count of numbers that are present at or before the index `i` in both `A` and `B`. | ||
|
||
Return the **prefix common array** of `A` and `B`. | ||
|
||
A sequence of `n` integers is called a **permutation** if it contains all integers from `1` to `n` exactly once. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: A = [1,3,2,4], B = [3,1,2,4] | ||
Output: [0,2,3,4] | ||
Explanation: At i = 0: no number is common, so C[0] = 0. | ||
At i = 1: 1 and 3 are common in A and B, so C[1] = 2. | ||
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3. | ||
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: A = [2,3,1], B = [3,1,2] | ||
Output: [0,1,3] | ||
Explanation: At i = 0: no number is common, so C[0] = 0. | ||
At i = 1: only 3 is common in A and B, so C[1] = 1. | ||
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
23 changes: 21 additions & 2 deletions
23
leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(A []int, B []int) []int { | ||
ans := make([]int, len(A)) | ||
count := make([]uint8, len(A)+1) | ||
for i := 0; i < len(A); i++ { | ||
if i > 0 { | ||
ans[i] = ans[i-1] | ||
} | ||
count[A[i]]++ | ||
count[B[i]]++ | ||
if A[i] == B[i] { | ||
ans[i]++ | ||
continue | ||
} | ||
if count[A[i]] == 2 { | ||
ans[i]++ | ||
} | ||
if count[B[i]] == 2 { | ||
ans[i]++ | ||
} | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters