Skip to content

Commit af27248

Browse files
authored
Merge pull request #1094 from 0xff-dev/2657
Add solution and test-cases for problem 2657
2 parents 4166a8a + 0f8737a commit af27248

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [2657.Find the Prefix Common Array of Two Arrays][title]
2+
3+
## Description
4+
You are given two **0-indexed** integer permutations `A` and `B` of length `n`.
5+
6+
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`.
7+
8+
Return the **prefix common array** of `A` and `B`.
9+
10+
A sequence of `n` integers is called a **permutation** if it contains all integers from `1` to `n` exactly once.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: A = [1,3,2,4], B = [3,1,2,4]
16+
Output: [0,2,3,4]
17+
Explanation: At i = 0: no number is common, so C[0] = 0.
18+
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
19+
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
20+
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: A = [2,3,1], B = [3,1,2]
27+
Output: [0,1,3]
28+
Explanation: At i = 0: no number is common, so C[0] = 0.
29+
At i = 1: only 3 is common in A and B, so C[1] = 1.
30+
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
31+
```
32+
33+
## 结语
34+
35+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
36+
37+
[title]: https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays
38+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(A []int, B []int) []int {
4+
ans := make([]int, len(A))
5+
count := make([]uint8, len(A)+1)
6+
for i := 0; i < len(A); i++ {
7+
if i > 0 {
8+
ans[i] = ans[i-1]
9+
}
10+
count[A[i]]++
11+
count[B[i]]++
12+
if A[i] == B[i] {
13+
ans[i]++
14+
continue
15+
}
16+
if count[A[i]] == 2 {
17+
ans[i]++
18+
}
19+
if count[B[i]] == 2 {
20+
ans[i]++
21+
}
22+
}
23+
return ans
524
}

leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
a, b []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 3, 2, 4}, []int{3, 1, 2, 4}, []int{0, 2, 3, 4}},
17+
{"TestCase2", []int{2, 3, 1}, []int{3, 1, 2}, []int{0, 1, 3}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.a, c.b)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.a, c.b)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)