Skip to content

Commit 5447162

Browse files
committed
feat: add solutions to lc problem: No.0923
No.0923.3Sum With Multiplicity
1 parent e3313bf commit 5447162

File tree

6 files changed

+255
-47
lines changed

6 files changed

+255
-47
lines changed

solution/0900-0999/0923.3Sum With Multiplicity/README.md

Lines changed: 95 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,126 @@ arr[i] = 1, arr[j] = arr[k] = 2 出现 12 次:
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:枚举**
54+
55+
我们先用一个长度为 $101$ 的数组 `cnt` 记录数组 `arr` 中每个元素出现的次数。
56+
57+
然后枚举数组 `arr` 中的元素作为三元组的第二个元素 $b$,先让 `cnt` 减去其中一个 $b$。接着从数组 `arr` 的开头开始枚举第一个元素 $a$,那么第三个元素 $c$ 为 $target - a - b$。如果 $c$ 的值在数组 `cnt` 的范围内,那么答案加上 $cnt[c]$。
58+
59+
注意答案的取模操作。
60+
61+
时间复杂度 $O(n^2)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `arr` 的长度,而 $C$ 为数组 `cnt` 的长度。本题中 $C = 101$。
62+
5363
<!-- tabs:start -->
5464

5565
### **Python3**
5666

5767
<!-- 这里可写当前语言的特殊实现逻辑 -->
5868

5969
```python
60-
70+
class Solution:
71+
def threeSumMulti(self, arr: List[int], target: int) -> int:
72+
cnt = Counter(arr)
73+
ans = 0
74+
mod = 10**9 + 7
75+
for j, b in enumerate(arr):
76+
cnt[b] -= 1
77+
for i in range(j):
78+
a = arr[i]
79+
c = target - a - b
80+
ans = (ans + cnt[c]) % mod
81+
return ans
6182
```
6283

6384
### **Java**
6485

6586
<!-- 这里可写当前语言的特殊实现逻辑 -->
6687

6788
```java
68-
89+
class Solution {
90+
private static final int MOD = (int) 1e9 + 7;
91+
92+
public int threeSumMulti(int[] arr, int target) {
93+
int[] cnt = new int[101];
94+
for (int v : arr) {
95+
++cnt[v];
96+
}
97+
long ans = 0;
98+
for (int j = 0; j < arr.length; ++j) {
99+
int b = arr[j];
100+
--cnt[b];
101+
for (int i = 0; i < j; ++i) {
102+
int a = arr[i];
103+
int c = target - a - b;
104+
if (c >= 0 && c <= 100) {
105+
ans = (ans + cnt[c]) % MOD;
106+
}
107+
}
108+
}
109+
return (int) ans;
110+
}
111+
}
69112
```
70113

71114
### **C++**
72115

73116
```cpp
74117
class Solution {
75118
public:
119+
const int mod = 1e9 + 7;
120+
76121
int threeSumMulti(vector<int>& arr, int target) {
77-
unordered_map<int, long> c;
78-
for (int a : arr) c[a]++;
79-
long res = 0;
80-
for (auto it : c)
81-
for (auto it2 : c) {
82-
int i = it.first, j = it2.first, k = target - i - j;
83-
if (!c.count(k)) continue;
84-
if (i == j && j == k)
85-
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
86-
else if (i == j && j != k)
87-
res += c[i] * (c[i] - 1) / 2 * c[k];
88-
else if (i < j && j < k)
89-
res += c[i] * c[j] * c[k];
122+
int cnt[101] = {0};
123+
for (int& v : arr) {
124+
++cnt[v];
125+
}
126+
long ans = 0;
127+
for (int j = 0; j < arr.size(); ++j) {
128+
int b = arr[j];
129+
--cnt[b];
130+
for (int i = 0; i < j; ++i) {
131+
int a = arr[i];
132+
int c = target - a - b;
133+
if (c >= 0 && c <= 100) {
134+
ans += cnt[c];
135+
ans %= mod;
136+
}
90137
}
91-
return res % int(1e9 + 7);
138+
}
139+
return ans;
92140
}
93141
};
94142
```
95143

144+
### **Go**
145+
146+
```go
147+
func threeSumMulti(arr []int, target int) int {
148+
const mod int = 1e9 + 7
149+
cnt := [101]int{}
150+
for _, v := range arr {
151+
cnt[v]++
152+
}
153+
ans := 0
154+
for j, b := range arr {
155+
cnt[b]--
156+
for i := 0; i < j; i++ {
157+
a := arr[i]
158+
c := target - a - b
159+
if c >= 0 && c <= 100 {
160+
ans += cnt[c]
161+
ans %= mod
162+
}
163+
}
164+
}
165+
return ans
166+
}
167+
```
168+
169+
### **...**
170+
171+
```
172+
173+
```
174+
96175
<!-- tabs:end -->

solution/0900-0999/0923.3Sum With Multiplicity/README_EN.md

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,107 @@ and two 2s from [2,2,2,2] in 6 ways.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def threeSumMulti(self, arr: List[int], target: int) -> int:
62+
cnt = Counter(arr)
63+
ans = 0
64+
mod = 10**9 + 7
65+
for j, b in enumerate(arr):
66+
cnt[b] -= 1
67+
for i in range(j):
68+
a = arr[i]
69+
c = target - a - b
70+
ans = (ans + cnt[c]) % mod
71+
return ans
6172
```
6273

6374
### **Java**
6475

6576
```java
66-
77+
class Solution {
78+
private static final int MOD = (int) 1e9 + 7;
79+
80+
public int threeSumMulti(int[] arr, int target) {
81+
int[] cnt = new int[101];
82+
for (int v : arr) {
83+
++cnt[v];
84+
}
85+
long ans = 0;
86+
for (int j = 0; j < arr.length; ++j) {
87+
int b = arr[j];
88+
--cnt[b];
89+
for (int i = 0; i < j; ++i) {
90+
int a = arr[i];
91+
int c = target - a - b;
92+
if (c >= 0 && c <= 100) {
93+
ans = (ans + cnt[c]) % MOD;
94+
}
95+
}
96+
}
97+
return (int) ans;
98+
}
99+
}
67100
```
68101

69102
### **C++**
70103

71104
```cpp
72105
class Solution {
73106
public:
107+
const int mod = 1e9 + 7;
108+
74109
int threeSumMulti(vector<int>& arr, int target) {
75-
unordered_map<int, long> c;
76-
for (int a : arr) c[a]++;
77-
long res = 0;
78-
for (auto it : c)
79-
for (auto it2 : c) {
80-
int i = it.first, j = it2.first, k = target - i - j;
81-
if (!c.count(k)) continue;
82-
if (i == j && j == k)
83-
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
84-
else if (i == j && j != k)
85-
res += c[i] * (c[i] - 1) / 2 * c[k];
86-
else if (i < j && j < k)
87-
res += c[i] * c[j] * c[k];
110+
int cnt[101] = {0};
111+
for (int& v : arr) {
112+
++cnt[v];
113+
}
114+
long ans = 0;
115+
for (int j = 0; j < arr.size(); ++j) {
116+
int b = arr[j];
117+
--cnt[b];
118+
for (int i = 0; i < j; ++i) {
119+
int a = arr[i];
120+
int c = target - a - b;
121+
if (c >= 0 && c <= 100) {
122+
ans += cnt[c];
123+
ans %= mod;
124+
}
88125
}
89-
return res % int(1e9 + 7);
126+
}
127+
return ans;
90128
}
91129
};
92130
```
93131

132+
### **Go**
133+
134+
```go
135+
func threeSumMulti(arr []int, target int) int {
136+
const mod int = 1e9 + 7
137+
cnt := [101]int{}
138+
for _, v := range arr {
139+
cnt[v]++
140+
}
141+
ans := 0
142+
for j, b := range arr {
143+
cnt[b]--
144+
for i := 0; i < j; i++ {
145+
a := arr[i]
146+
c := target - a - b
147+
if c >= 0 && c <= 100 {
148+
ans += cnt[c]
149+
ans %= mod
150+
}
151+
}
152+
}
153+
return ans
154+
}
155+
```
156+
157+
### **...**
158+
159+
```
160+
161+
```
162+
94163
<!-- tabs:end -->
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
class Solution {
22
public:
3+
const int mod = 1e9 + 7;
4+
35
int threeSumMulti(vector<int>& arr, int target) {
4-
unordered_map<int, long> c;
5-
for (int a : arr) c[a]++;
6-
long res = 0;
7-
for (auto it : c)
8-
for (auto it2 : c) {
9-
int i = it.first, j = it2.first, k = target - i - j;
10-
if (!c.count(k)) continue;
11-
if (i == j && j == k)
12-
res += c[i] * (c[i] - 1) * (c[i] - 2) / 6;
13-
else if (i == j && j != k)
14-
res += c[i] * (c[i] - 1) / 2 * c[k];
15-
else if (i < j && j < k)
16-
res += c[i] * c[j] * c[k];
6+
int cnt[101] = {0};
7+
for (int& v : arr) {
8+
++cnt[v];
9+
}
10+
long ans = 0;
11+
for (int j = 0; j < arr.size(); ++j) {
12+
int b = arr[j];
13+
--cnt[b];
14+
for (int i = 0; i < j; ++i) {
15+
int a = arr[i];
16+
int c = target - a - b;
17+
if (c >= 0 && c <= 100) {
18+
ans += cnt[c];
19+
ans %= mod;
20+
}
1721
}
18-
return res % int(1e9 + 7);
22+
}
23+
return ans;
1924
}
20-
};
25+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func threeSumMulti(arr []int, target int) int {
2+
const mod int = 1e9 + 7
3+
cnt := [101]int{}
4+
for _, v := range arr {
5+
cnt[v]++
6+
}
7+
ans := 0
8+
for j, b := range arr {
9+
cnt[b]--
10+
for i := 0; i < j; i++ {
11+
a := arr[i]
12+
c := target - a - b
13+
if c >= 0 && c <= 100 {
14+
ans += cnt[c]
15+
ans %= mod
16+
}
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int threeSumMulti(int[] arr, int target) {
5+
int[] cnt = new int[101];
6+
for (int v : arr) {
7+
++cnt[v];
8+
}
9+
long ans = 0;
10+
for (int j = 0; j < arr.length; ++j) {
11+
int b = arr[j];
12+
--cnt[b];
13+
for (int i = 0; i < j; ++i) {
14+
int a = arr[i];
15+
int c = target - a - b;
16+
if (c >= 0 && c <= 100) {
17+
ans = (ans + cnt[c]) % MOD;
18+
}
19+
}
20+
}
21+
return (int) ans;
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def threeSumMulti(self, arr: List[int], target: int) -> int:
3+
cnt = Counter(arr)
4+
ans = 0
5+
mod = 10**9 + 7
6+
for j, b in enumerate(arr):
7+
cnt[b] -= 1
8+
for i in range(j):
9+
a = arr[i]
10+
c = target - a - b
11+
ans = (ans + cnt[c]) % mod
12+
return ans

0 commit comments

Comments
 (0)