diff --git a/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/README.md b/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/README.md index 17f3b3006..c0a9548e1 100755 --- a/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/README.md +++ b/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/README.md @@ -1,28 +1,36 @@ # [1865.Finding Pairs With a Certain Sum][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two integer arrays `nums1` and `nums2`. You are tasked to implement a data structure that supports queries of two types: -**Example 1:** +1. **Add** a positive integer to an element of a given index in the array `nums2`. +2. **Count** the number of pairs `(i, j)` such that `nums1[i] + nums2[j]` equals a given value (`0 <= i < nums1.length` and `0 <= j < nums2.length`). -``` -Input: a = "11", b = "1" -Output: "100" -``` +Implement the `FindSumPairs` class: -## 题意 -> ... +- `FindSumPairs(int[] nums1, int[] nums2)` Initializes the `FindSumPairs` object with two integer arrays `nums1` and `nums2`. +- `void add(int index, int val)` Adds `val` to `nums2[index]`, i.e., apply `nums2[index] += val`. +- `int count(int tot)` Returns the number of pairs `(i, j)` such that `nums1[i] + nums2[j] == tot`. -## 题解 +**Example 1:** -### 思路1 -> ... -Finding Pairs With a Certain Sum -```go ``` - +Input +["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"] +[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]] +Output +[null, 8, null, 2, 1, null, null, 11] + +Explanation +FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]); +findSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4 +findSumPairs.add(3, 2); // now nums2 = [1,4,5,4,5,4] +findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5 +findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1 +findSumPairs.add(0, 1); // now nums2 = [2,4,5,4,5,4] +findSumPairs.add(1, 1); // now nums2 = [2,5,5,4,5,4] +findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4 +``` ## 结语 diff --git a/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution.go b/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution.go index d115ccf5e..173e612fb 100644 --- a/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution.go +++ b/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution.go @@ -1,5 +1,58 @@ package Solution -func Solution(x bool) bool { - return x +type FindSumPairs struct { + c1, c2 map[int]int + n2 []int +} + +func Constructor(nums1 []int, nums2 []int) FindSumPairs { + f := FindSumPairs{ + c1: map[int]int{}, c2: map[int]int{}, n2: nums2, + } + for _, n := range nums1 { + f.c1[n]++ + } + for _, n := range nums2 { + f.c2[n]++ + } + return f +} + +func (this *FindSumPairs) Add(index int, val int) { + source := this.n2[index] + this.n2[index] += val + this.c2[source]-- + if this.c2[source] == 0 { + delete(this.c2, source) + } + this.c2[this.n2[index]]++ +} + +func (this *FindSumPairs) Count(tot int) int { + ans := 0 + for k, c := range this.c1 { + if c1, ok := this.c2[tot-k]; ok { + ans += c * c1 + } + } + return ans +} + +type op struct { + name byte + a, b int +} + +func Solution(nums1, nums2 []int, ops []op) []int { + var ans []int + c := Constructor(nums1, nums2) + for _, o := range ops { + if o.name == 'a' { + c.Add(o.a, o.b) + continue + } + ans = append(ans, c.Count(o.a)) + } + return ans + } diff --git a/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution_test.go b/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution_test.go index 14ff50eb4..6eceeec53 100644 --- a/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution_test.go +++ b/leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution_test.go @@ -9,31 +9,52 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums1, nums2 []int + ops []op + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 1, 2, 2, 2, 3}, []int{1, 4, 5, 2, 5, 4}, []op{ + { + name: 'c', a: 7, + }, + { + name: 'a', a: 3, b: 2, + }, + { + name: 'c', a: 8, + }, + { + name: 'c', a: 4, + }, + { + name: 'a', a: 0, b: 1, + }, + { + name: 'a', a: 1, b: 1, + }, + { + name: 'c', a: 7, + }, + }, []int{8, 2, 1, 11}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums1, c.nums2, c.ops) 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 %v", + c.expect, got, c.nums1, c.nums2, c.ops) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }