Skip to content

Commit 71f0c65

Browse files
authored
Merge pull request #1253 from 0xff-dev/1865
Add solution and test-cases for problem 1865
2 parents a1c271a + 5838e85 commit 71f0c65

File tree

3 files changed

+111
-29
lines changed

3 files changed

+111
-29
lines changed

leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1865.Finding Pairs With a Certain Sum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two integer arrays `nums1` and `nums2`. You are tasked to implement a data structure that supports queries of two types:
75

8-
**Example 1:**
6+
1. **Add** a positive integer to an element of a given index in the array `nums2`.
7+
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`).
98

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
Implement the `FindSumPairs` class:
1410

15-
## 题意
16-
> ...
11+
- `FindSumPairs(int[] nums1, int[] nums2)` Initializes the `FindSumPairs` object with two integer arrays `nums1` and `nums2`.
12+
- `void add(int index, int val)` Adds `val` to `nums2[index]`, i.e., apply `nums2[index] += val`.
13+
- `int count(int tot)` Returns the number of pairs `(i, j)` such that `nums1[i] + nums2[j] == tot`.
1714

18-
## 题解
15+
**Example 1:**
1916

20-
### 思路1
21-
> ...
22-
Finding Pairs With a Certain Sum
23-
```go
2417
```
25-
18+
Input
19+
["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
20+
[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
21+
Output
22+
[null, 8, null, 2, 1, null, null, 11]
23+
24+
Explanation
25+
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
26+
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
27+
findSumPairs.add(3, 2); // now nums2 = [1,4,5,4,5,4]
28+
findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5
29+
findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1
30+
findSumPairs.add(0, 1); // now nums2 = [2,4,5,4,5,4]
31+
findSumPairs.add(1, 1); // now nums2 = [2,5,5,4,5,4]
32+
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
33+
```
2634

2735
## 结语
2836

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type FindSumPairs struct {
4+
c1, c2 map[int]int
5+
n2 []int
6+
}
7+
8+
func Constructor(nums1 []int, nums2 []int) FindSumPairs {
9+
f := FindSumPairs{
10+
c1: map[int]int{}, c2: map[int]int{}, n2: nums2,
11+
}
12+
for _, n := range nums1 {
13+
f.c1[n]++
14+
}
15+
for _, n := range nums2 {
16+
f.c2[n]++
17+
}
18+
return f
19+
}
20+
21+
func (this *FindSumPairs) Add(index int, val int) {
22+
source := this.n2[index]
23+
this.n2[index] += val
24+
this.c2[source]--
25+
if this.c2[source] == 0 {
26+
delete(this.c2, source)
27+
}
28+
this.c2[this.n2[index]]++
29+
}
30+
31+
func (this *FindSumPairs) Count(tot int) int {
32+
ans := 0
33+
for k, c := range this.c1 {
34+
if c1, ok := this.c2[tot-k]; ok {
35+
ans += c * c1
36+
}
37+
}
38+
return ans
39+
}
40+
41+
type op struct {
42+
name byte
43+
a, b int
44+
}
45+
46+
func Solution(nums1, nums2 []int, ops []op) []int {
47+
var ans []int
48+
c := Constructor(nums1, nums2)
49+
for _, o := range ops {
50+
if o.name == 'a' {
51+
c.Add(o.a, o.b)
52+
continue
53+
}
54+
ans = append(ans, c.Count(o.a))
55+
}
56+
return ans
57+
558
}

leetcode/1801-1900/1865.Finding-Pairs-With-a-Certain-Sum/Solution_test.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,52 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums1, nums2 []int
14+
ops []op
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 1, 2, 2, 2, 3}, []int{1, 4, 5, 2, 5, 4}, []op{
18+
{
19+
name: 'c', a: 7,
20+
},
21+
{
22+
name: 'a', a: 3, b: 2,
23+
},
24+
{
25+
name: 'c', a: 8,
26+
},
27+
{
28+
name: 'c', a: 4,
29+
},
30+
{
31+
name: 'a', a: 0, b: 1,
32+
},
33+
{
34+
name: 'a', a: 1, b: 1,
35+
},
36+
{
37+
name: 'c', a: 7,
38+
},
39+
}, []int{8, 2, 1, 11}},
1940
}
2041

2142
// 开始测试
2243
for i, c := range cases {
2344
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
45+
got := Solution(c.nums1, c.nums2, c.ops)
2546
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
47+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
48+
c.expect, got, c.nums1, c.nums2, c.ops)
2849
}
2950
})
3051
}
3152
}
3253

33-
// 压力测试
54+
// 压力测试
3455
func BenchmarkSolution(b *testing.B) {
3556
}
3657

37-
// 使用案列
58+
// 使用案列
3859
func ExampleSolution() {
3960
}

0 commit comments

Comments
 (0)