-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum-with-multiplicity.go
60 lines (50 loc) · 1.28 KB
/
3sum-with-multiplicity.go
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
58
59
60
package main
import (
"fmt"
)
// source: https://leetcode.com/problems/3sum-with-multiplicity/
func threeSumMulti(arr []int, target int) int {
const maxVal = 100
var valCnt [maxVal + 1]int
var k, res int
for _, v := range arr {
valCnt[v]++
}
for i := 0; i <= maxVal; i++ {
if valCnt[i] == 0 {
continue
}
for j := i; j <= maxVal; j++ {
k = target - i - j
if valCnt[j] == 0 || k > 100 || k < 0 || valCnt[k] == 0 {
continue
}
if i == j && j == k {
res += valCnt[i] * (valCnt[i] - 1) * (valCnt[i] - 2) / 6
} else if i == j && j != k {
res += valCnt[i] * (valCnt[i] - 1) / 2 * valCnt[k]
} else if j < k {
res += valCnt[i] * valCnt[j] * valCnt[k]
}
}
}
return res % (1e9 + 7)
}
func main() {
// Example 1
var arr1 = []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}
var target1 int = 8
fmt.Println("Expected: 20 Output: ", threeSumMulti(arr1, target1))
// Example 2
var arr2 = []int{1, 1, 2, 2, 2, 2}
var target2 int = 5
fmt.Println("Expected: 12 Output: ", threeSumMulti(arr2, target2))
// Example 3
var arr3 = []int{1, 2, 1}
var target3 int = 5
fmt.Println("Expected: 0 Output: ", threeSumMulti(arr3, target3))
// Example 4
var arr4 = []int{1, 3, 2, 1}
var target4 int = 6
fmt.Println("Expected: 2 Output: ", threeSumMulti(arr4, target4))
}