diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/README.md b/solution/0900-0999/0923.3Sum With Multiplicity/README.md index 8bb4e96b6d8cf..c45b367e28088 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/README.md +++ b/solution/0900-0999/0923.3Sum With Multiplicity/README.md @@ -50,28 +50,27 @@ arr[i] = 1, arr[j] = arr[k] = 2 出现 12 次: ## 解法 -### 方法一:枚举 +### 方法一:计数 + 枚举 -我们先用一个长度为 $101$ 的数组 `cnt` 记录数组 `arr` 中每个元素出现的次数。 +我们可以用一个哈希表或者一个长度为 $101$ 的数组 $cnt$ 统计数组 $arr$ 中每个元素的出现次数。 -然后枚举数组 `arr` 中的元素作为三元组的第二个元素 $b$,先让 `cnt` 减去其中一个 $b$。接着从数组 `arr` 的开头开始枚举第一个元素 $a$,那么第三个元素 $c$ 为 $target - a - b$。如果 $c$ 的值在数组 `cnt` 的范围内,那么答案加上 $cnt[c]$。 +然后,我们枚举数组 $arr$ 中的每个元素 $arr[j]$,先将 $cnt[arr[j]]$ 减一,然后再枚举 $arr[j]$ 之前的元素 $arr[i]$,计算 $c = target - arr[i] - arr[j]$,如果 $c$ 在 $[0, 100]$ 的范围内,那么答案就加上 $cnt[c]$,最后返回答案。 -注意答案的取模操作。 +注意,这里的答案可能会超过 ${10}^9 + 7$,所以在每次加法操作后都要取模。 -时间复杂度 $O(n^2)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `arr` 的长度;而 $C$ 为数组 `cnt` 的长度,本题中 $C = 101$。 +时间复杂度 $O(n^2)$,其中 $n$ 为数组 $arr$ 的长度。空间复杂度 $O(C)$,其中 $C$ 为数组 $arr$ 中元素的最大值,本题中 $C = 100$。 ```python class Solution: def threeSumMulti(self, arr: List[int], target: int) -> int: + mod = 10**9 + 7 cnt = Counter(arr) ans = 0 - mod = 10**9 + 7 for j, b in enumerate(arr): cnt[b] -= 1 - for i in range(j): - a = arr[i] + for a in arr[:j]: c = target - a - b ans = (ans + cnt[c]) % mod return ans @@ -79,26 +78,24 @@ class Solution: ```java class Solution { - private static final int MOD = (int) 1e9 + 7; - public int threeSumMulti(int[] arr, int target) { + final int mod = (int) 1e9 + 7; int[] cnt = new int[101]; - for (int v : arr) { - ++cnt[v]; + for (int x : arr) { + ++cnt[x]; } - long ans = 0; - for (int j = 0; j < arr.length; ++j) { - int b = arr[j]; - --cnt[b]; + int n = arr.length; + int ans = 0; + for (int j = 0; j < n; ++j) { + --cnt[arr[j]]; for (int i = 0; i < j; ++i) { - int a = arr[i]; - int c = target - a - b; - if (c >= 0 && c <= 100) { - ans = (ans + cnt[c]) % MOD; + int c = target - arr[i] - arr[j]; + if (c >= 0 && c < cnt.length) { + ans = (ans + cnt[c]) % mod; } } } - return (int) ans; + return ans; } } ``` @@ -106,23 +103,20 @@ class Solution { ```cpp class Solution { public: - const int mod = 1e9 + 7; - int threeSumMulti(vector& arr, int target) { - int cnt[101] = {0}; - for (int& v : arr) { - ++cnt[v]; + const int mod = 1e9 + 7; + int cnt[101]{}; + for (int x : arr) { + ++cnt[x]; } - long ans = 0; - for (int j = 0; j < arr.size(); ++j) { - int b = arr[j]; - --cnt[b]; + int n = arr.size(); + int ans = 0; + for (int j = 0; j < n; ++j) { + --cnt[arr[j]]; for (int i = 0; i < j; ++i) { - int a = arr[i]; - int c = target - a - b; + int c = target - arr[i] - arr[j]; if (c >= 0 && c <= 100) { - ans += cnt[c]; - ans %= mod; + ans = (ans + cnt[c]) % mod; } } } @@ -132,25 +126,43 @@ public: ``` ```go -func threeSumMulti(arr []int, target int) int { +func threeSumMulti(arr []int, target int) (ans int) { const mod int = 1e9 + 7 cnt := [101]int{} - for _, v := range arr { - cnt[v]++ + for _, x := range arr { + cnt[x]++ } - ans := 0 for j, b := range arr { cnt[b]-- - for i := 0; i < j; i++ { - a := arr[i] - c := target - a - b - if c >= 0 && c <= 100 { - ans += cnt[c] - ans %= mod + for _, a := range arr[:j] { + if c := target - a - b; c >= 0 && c < len(cnt) { + ans = (ans + cnt[c]) % mod } } } - return ans + return +} +``` + +```ts +function threeSumMulti(arr: number[], target: number): number { + const mod = 10 ** 9 + 7; + const cnt: number[] = Array(101).fill(0); + for (const x of arr) { + ++cnt[x]; + } + let ans = 0; + const n = arr.length; + for (let j = 0; j < n; ++j) { + --cnt[arr[j]]; + for (let i = 0; i < j; ++i) { + const c = target - arr[i] - arr[j]; + if (c >= 0 && c < cnt.length) { + ans = (ans + cnt[c]) % mod; + } + } + } + return ans; } ``` diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md b/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md index c0f44dc563820..7e347875abe71 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md +++ b/solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md @@ -54,28 +54,27 @@ and two 2s from [2,2,2,2] in 6 ways. ## Solutions -### Solution 1: Enumeration +### Solution 1: Counting + Enumeration -First, we use an array `cnt` of length $101$ to record the number of occurrences of each element in the array `arr`. +We can use a hash table or an array $cnt$ of length $101$ to count the occurrence of each element in the array $arr$. -Then, we enumerate the elements in the array `arr` as the second element $b$ of the triplet, and first subtract one $b$ from `cnt`. Then, starting from the beginning of the array `arr`, we enumerate the first element $a$, and the third element $c$ is $target - a - b$. If the value of $c$ is within the range of the array `cnt`, then the answer is increased by $cnt[c]$. +Then, we enumerate each element $arr[j]$ in the array $arr$, first subtract one from $cnt[arr[j]]$, and then enumerate the elements $arr[i]$ before $arr[j]$, calculate $c = target - arr[i] - arr[j]$. If $c$ is in the range of $[0, 100]$, then the answer is increased by $cnt[c]$, and finally return the answer. -Note the modulo operation of the answer. +Note that the answer may exceed ${10}^9 + 7$, so take the modulus after each addition operation. -The time complexity is $O(n^2)$, and the space complexity is $O(C)$. Where $n$ is the length of the array `arr`; and $C$ is the length of the array `cnt`, in this problem $C = 101$. +The time complexity is $O(n^2)$, where $n$ is the length of the array $arr$. The space complexity is $O(C)$, where $C$ is the maximum value of the elements in the array $arr$, in this problem $C = 100$. ```python class Solution: def threeSumMulti(self, arr: List[int], target: int) -> int: + mod = 10**9 + 7 cnt = Counter(arr) ans = 0 - mod = 10**9 + 7 for j, b in enumerate(arr): cnt[b] -= 1 - for i in range(j): - a = arr[i] + for a in arr[:j]: c = target - a - b ans = (ans + cnt[c]) % mod return ans @@ -83,26 +82,24 @@ class Solution: ```java class Solution { - private static final int MOD = (int) 1e9 + 7; - public int threeSumMulti(int[] arr, int target) { + final int mod = (int) 1e9 + 7; int[] cnt = new int[101]; - for (int v : arr) { - ++cnt[v]; + for (int x : arr) { + ++cnt[x]; } - long ans = 0; - for (int j = 0; j < arr.length; ++j) { - int b = arr[j]; - --cnt[b]; + int n = arr.length; + int ans = 0; + for (int j = 0; j < n; ++j) { + --cnt[arr[j]]; for (int i = 0; i < j; ++i) { - int a = arr[i]; - int c = target - a - b; - if (c >= 0 && c <= 100) { - ans = (ans + cnt[c]) % MOD; + int c = target - arr[i] - arr[j]; + if (c >= 0 && c < cnt.length) { + ans = (ans + cnt[c]) % mod; } } } - return (int) ans; + return ans; } } ``` @@ -110,23 +107,20 @@ class Solution { ```cpp class Solution { public: - const int mod = 1e9 + 7; - int threeSumMulti(vector& arr, int target) { - int cnt[101] = {0}; - for (int& v : arr) { - ++cnt[v]; + const int mod = 1e9 + 7; + int cnt[101]{}; + for (int x : arr) { + ++cnt[x]; } - long ans = 0; - for (int j = 0; j < arr.size(); ++j) { - int b = arr[j]; - --cnt[b]; + int n = arr.size(); + int ans = 0; + for (int j = 0; j < n; ++j) { + --cnt[arr[j]]; for (int i = 0; i < j; ++i) { - int a = arr[i]; - int c = target - a - b; + int c = target - arr[i] - arr[j]; if (c >= 0 && c <= 100) { - ans += cnt[c]; - ans %= mod; + ans = (ans + cnt[c]) % mod; } } } @@ -136,25 +130,43 @@ public: ``` ```go -func threeSumMulti(arr []int, target int) int { +func threeSumMulti(arr []int, target int) (ans int) { const mod int = 1e9 + 7 cnt := [101]int{} - for _, v := range arr { - cnt[v]++ + for _, x := range arr { + cnt[x]++ } - ans := 0 for j, b := range arr { cnt[b]-- - for i := 0; i < j; i++ { - a := arr[i] - c := target - a - b - if c >= 0 && c <= 100 { - ans += cnt[c] - ans %= mod + for _, a := range arr[:j] { + if c := target - a - b; c >= 0 && c < len(cnt) { + ans = (ans + cnt[c]) % mod } } } - return ans + return +} +``` + +```ts +function threeSumMulti(arr: number[], target: number): number { + const mod = 10 ** 9 + 7; + const cnt: number[] = Array(101).fill(0); + for (const x of arr) { + ++cnt[x]; + } + let ans = 0; + const n = arr.length; + for (let j = 0; j < n; ++j) { + --cnt[arr[j]]; + for (let i = 0; i < j; ++i) { + const c = target - arr[i] - arr[j]; + if (c >= 0 && c < cnt.length) { + ans = (ans + cnt[c]) % mod; + } + } + } + return ans; } ``` diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.cpp b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.cpp index a22452c46b064..7e214c8a9e274 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.cpp +++ b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.cpp @@ -1,22 +1,19 @@ class Solution { public: - const int mod = 1e9 + 7; - int threeSumMulti(vector& arr, int target) { - int cnt[101] = {0}; - for (int& v : arr) { - ++cnt[v]; + const int mod = 1e9 + 7; + int cnt[101]{}; + for (int x : arr) { + ++cnt[x]; } - long ans = 0; - for (int j = 0; j < arr.size(); ++j) { - int b = arr[j]; - --cnt[b]; + int n = arr.size(); + int ans = 0; + for (int j = 0; j < n; ++j) { + --cnt[arr[j]]; for (int i = 0; i < j; ++i) { - int a = arr[i]; - int c = target - a - b; + int c = target - arr[i] - arr[j]; if (c >= 0 && c <= 100) { - ans += cnt[c]; - ans %= mod; + ans = (ans + cnt[c]) % mod; } } } diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.go b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.go index 2f2eed07775d9..edb92cc94dcac 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.go +++ b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.go @@ -1,20 +1,16 @@ -func threeSumMulti(arr []int, target int) int { +func threeSumMulti(arr []int, target int) (ans int) { const mod int = 1e9 + 7 cnt := [101]int{} - for _, v := range arr { - cnt[v]++ + for _, x := range arr { + cnt[x]++ } - ans := 0 for j, b := range arr { cnt[b]-- - for i := 0; i < j; i++ { - a := arr[i] - c := target - a - b - if c >= 0 && c <= 100 { - ans += cnt[c] - ans %= mod + for _, a := range arr[:j] { + if c := target - a - b; c >= 0 && c < len(cnt) { + ans = (ans + cnt[c]) % mod } } } - return ans + return } \ No newline at end of file diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.java b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.java index fc318885a2955..99cd928f72b85 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.java +++ b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.java @@ -1,23 +1,21 @@ class Solution { - private static final int MOD = (int) 1e9 + 7; - public int threeSumMulti(int[] arr, int target) { + final int mod = (int) 1e9 + 7; int[] cnt = new int[101]; - for (int v : arr) { - ++cnt[v]; + for (int x : arr) { + ++cnt[x]; } - long ans = 0; - for (int j = 0; j < arr.length; ++j) { - int b = arr[j]; - --cnt[b]; + int n = arr.length; + int ans = 0; + for (int j = 0; j < n; ++j) { + --cnt[arr[j]]; for (int i = 0; i < j; ++i) { - int a = arr[i]; - int c = target - a - b; - if (c >= 0 && c <= 100) { - ans = (ans + cnt[c]) % MOD; + int c = target - arr[i] - arr[j]; + if (c >= 0 && c < cnt.length) { + ans = (ans + cnt[c]) % mod; } } } - return (int) ans; + return ans; } } \ No newline at end of file diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.py b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.py index 3ca2db829d9af..c4e4c07cb005a 100644 --- a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.py +++ b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.py @@ -1,12 +1,11 @@ class Solution: def threeSumMulti(self, arr: List[int], target: int) -> int: + mod = 10**9 + 7 cnt = Counter(arr) ans = 0 - mod = 10**9 + 7 for j, b in enumerate(arr): cnt[b] -= 1 - for i in range(j): - a = arr[i] + for a in arr[:j]: c = target - a - b ans = (ans + cnt[c]) % mod return ans diff --git a/solution/0900-0999/0923.3Sum With Multiplicity/Solution.ts b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.ts new file mode 100644 index 0000000000000..19a3f254d711d --- /dev/null +++ b/solution/0900-0999/0923.3Sum With Multiplicity/Solution.ts @@ -0,0 +1,19 @@ +function threeSumMulti(arr: number[], target: number): number { + const mod = 10 ** 9 + 7; + const cnt: number[] = Array(101).fill(0); + for (const x of arr) { + ++cnt[x]; + } + let ans = 0; + const n = arr.length; + for (let j = 0; j < n; ++j) { + --cnt[arr[j]]; + for (let i = 0; i < j; ++i) { + const c = target - arr[i] - arr[j]; + if (c >= 0 && c < cnt.length) { + ans = (ans + cnt[c]) % mod; + } + } + } + return ans; +}