diff --git a/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/README.md b/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/README.md new file mode 100644 index 000000000..56e60d0e9 --- /dev/null +++ b/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/README.md @@ -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 diff --git a/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution.go b/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution.go index d115ccf5e..fceb92fa5 100755 --- a/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution.go +++ b/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution.go @@ -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 } diff --git a/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution_test.go b/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution_test.go index 14ff50eb4..e93ed6691 100755 --- a/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution_test.go +++ b/leetcode/2601-2700/2657.Find-the-Prefix-Common-Array-of-Two-Arrays/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + a, b []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 3, 2, 4}, []int{3, 1, 2, 4}, []int{0, 2, 3, 4}}, + {"TestCase2", []int{2, 3, 1}, []int{3, 1, 2}, []int{0, 1, 3}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.a, c.b) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.a, c.b) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }