diff --git a/leetcode/901-1000/0923.3Sum-With-Multiplicity/README.md b/leetcode/901-1000/0923.3Sum-With-Multiplicity/README.md index 9624c9288..732f3090e 100644 --- a/leetcode/901-1000/0923.3Sum-With-Multiplicity/README.md +++ b/leetcode/901-1000/0923.3Sum-With-Multiplicity/README.md @@ -1,28 +1,41 @@ # [923.3Sum With Multiplicity][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 +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`. + +As the answer can be very large, return it **modulo** `10^9 + 7`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8 +Output: 20 +Explanation: +Enumerating by the values (arr[i], arr[j], arr[k]): +(1, 2, 5) occurs 8 times; +(1, 3, 4) occurs 8 times; +(2, 2, 4) occurs 2 times; +(2, 3, 3) occurs 2 times. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -3Sum With Multiplicity -```go +``` +Input: arr = [1,1,2,2,2,2], target = 5 +Output: 12 +Explanation: +arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times: +We choose one 1 from [1,1] in 2 ways, +and two 2s from [2,2,2,2] in 6 ways. ``` +**Example 3:** + +``` +Input: arr = [2,1,3], target = 6 +Output: 1 +Explanation: (1, 2, 3) occured one time in the array so we return 1. +``` ## 结语 diff --git a/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution.go b/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution.go index d115ccf5e..94aab1a50 100644 --- a/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution.go +++ b/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution.go @@ -1,5 +1,25 @@ package Solution -func Solution(x bool) bool { - return x +const mod923 = 1000000007 + +func Solution(arr []int, target int) int { + dp := make([][101]int, len(arr)) + dp[0][arr[0]] = 1 + for i := 1; i < len(arr); i++ { + for j := 0; j < 101; j++ { + dp[i][j] = dp[i-1][j] + } + dp[i][arr[i]]++ + } + + ans := 0 + for i := 2; i < len(arr); i++ { + cur := arr[i] + for pre := i - 1; pre > 0; pre-- { + if diff := target - arr[pre] - cur; diff >= 0 && diff <= 100 { + ans = (ans + dp[pre-1][diff]) % mod923 + } + } + } + return ans } diff --git a/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution_test.go b/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution_test.go index 14ff50eb4..e66f9d93d 100644 --- a/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution_test.go +++ b/leetcode/901-1000/0923.3Sum-With-Multiplicity/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + target int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 8, 20}, + {"TestCase2", []int{1, 1, 2, 2, 2, 2}, 5, 12}, + {"TestCase3", []int{2, 1, 3}, 6, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.target) 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.inputs, c.target) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }