Skip to content

Commit 69fb488

Browse files
authored
Merge pull request #1068 from 0xff-dev/923
Add solution and test-cases for problem 923
2 parents dbd4dda + 06779af commit 69fb488

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

leetcode/901-1000/0923.3Sum-With-Multiplicity/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [923.3Sum With Multiplicity][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+
Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.
5+
6+
As the answer can be very large, return it **modulo** `10^9 + 7`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
12+
Output: 20
13+
Explanation:
14+
Enumerating by the values (arr[i], arr[j], arr[k]):
15+
(1, 2, 5) occurs 8 times;
16+
(1, 3, 4) occurs 8 times;
17+
(2, 2, 4) occurs 2 times;
18+
(2, 3, 3) occurs 2 times.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
3Sum With Multiplicity
23-
```go
23+
```
24+
Input: arr = [1,1,2,2,2,2], target = 5
25+
Output: 12
26+
Explanation:
27+
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
28+
We choose one 1 from [1,1] in 2 ways,
29+
and two 2s from [2,2,2,2] in 6 ways.
2430
```
2531

32+
**Example 3:**
33+
34+
```
35+
Input: arr = [2,1,3], target = 6
36+
Output: 1
37+
Explanation: (1, 2, 3) occured one time in the array so we return 1.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
const mod923 = 1000000007
4+
5+
func Solution(arr []int, target int) int {
6+
dp := make([][101]int, len(arr))
7+
dp[0][arr[0]] = 1
8+
for i := 1; i < len(arr); i++ {
9+
for j := 0; j < 101; j++ {
10+
dp[i][j] = dp[i-1][j]
11+
}
12+
dp[i][arr[i]]++
13+
}
14+
15+
ans := 0
16+
for i := 2; i < len(arr); i++ {
17+
cur := arr[i]
18+
for pre := i - 1; pre > 0; pre-- {
19+
if diff := target - arr[pre] - cur; diff >= 0 && diff <= 100 {
20+
ans = (ans + dp[pre-1][diff]) % mod923
21+
}
22+
}
23+
}
24+
return ans
525
}

leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
target int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 8, 20},
18+
{"TestCase2", []int{1, 1, 2, 2, 2, 2}, 5, 12},
19+
{"TestCase3", []int{2, 1, 3}, 6, 1},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.target)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.target)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)