-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum_With_Multiplicity.Py
More file actions
49 lines (43 loc) · 1.43 KB
/
3Sum_With_Multiplicity.Py
File metadata and controls
49 lines (43 loc) · 1.43 KB
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
'''Problem : 3Sum With Multiplicity '''
#CODE :
MOD = 10 ** 9 + 7
class Solution:
def threeSumMulti(self, A: List[int], target: int) -> int:
"""
Adapted from 3 sum
3 pointers O(N + K^2)
j, k scan each element once
"""
counter = defaultdict(int)
for a in A:
counter[a] += 1
keys = list(counter.keys())
keys.sort()
n = len(keys)
ret = 0
for i in range(n):
j = i # not i + 1
k = n - 1
while j <= k: # not <
a, b, c = keys[i], keys[j], keys[k]
if b + c < target - a:
j += 1
elif b + c > target - a:
k -= 1
else: # equal
if a < b < c:
ret += counter[a] * counter[b] * counter[c]
elif a == b < c:
# nC2
ret += counter[a] * (counter[a] - 1) // 2 * counter[c]
elif a < b == c:
ret += counter[a] * counter[b] * (counter[b] - 1) // 2
elif a== b == c:
# nC3
ret += counter[a] * (counter[a] - 1) * (counter[a] - 2) // (3 * 2)
else:
raise
ret %= MOD
j += 1
k -= 1
return ret