-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path3SumWithMultiplicity.java
57 lines (54 loc) · 1.48 KB
/
3SumWithMultiplicity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*https://leetcode.com/problems/3sum-with-multiplicity/*/
class Solution {
public int threeSumMulti(int[] arr, int target) {
int m = (int)1e9+7;
long[] count = new long[101];
for (int num : arr)
++count[num];
long result = 0;
int a, b, c;
for (a = 0; a <= 100; ++a)
{
for (b = a+1; b <= 100; ++b)
{
c = target-a-b;
if (b < c && c <= 100)
{
result += count[a]*count[b]*count[c];
result %= m;
}
}
}
for (a = 0; a <= 100; ++a)
{
c = target-2*a;
if (a < c && c <= 100 && count[a] >= 2)
{
result += count[a]*(count[a]-1)*count[c]/2;
result %= m;
}
}
for (a = 0; a <= 100; ++a)
{
if ((target-a)%2 == 0)
{
c = (target-a)/2;
if (a < c && c <= 100 && count[c] >= 2)
{
result += count[a]*count[c]*(count[c]-1)/2;
result %= m;
}
}
}
if (target%3 == 0)
{
a = target/3;
if (0 <= a && a <= 100 && count[a] >= 3)
{
result += count[a]*(count[a]-1)*(count[a]-2)/6;
result %= m;
}
}
return (int)result;
}
}