Skip to content

Instantly share code, notes, and snippets.

@liketheflower
Last active December 11, 2019 17:01
Show Gist options
  • Save liketheflower/0e9bb48a3ab50a07026b55f64846ff72 to your computer and use it in GitHub Desktop.
Save liketheflower/0e9bb48a3ab50a07026b55f64846ff72 to your computer and use it in GitHub Desktop.
class Solution:
def threeSumMulti(self, A: List[int], target: int) -> int:
cnt = collections.Counter(A)
res = 0
MOD = 10**9+7
for a, b, c in itertools.combinations(cnt.keys(), 3):
if a+b+c == target:
res += cnt[a]*cnt[b]*cnt[c] % MOD
for a, b in itertools.combinations(cnt.keys(), 2):
if 2*a+b==target and cnt[a]>=2:
res += (cnt[a]*(cnt[a]-1)//2)*cnt[b] % MOD
if a+2*b==target and cnt[b]>=2:
res += (cnt[b]*(cnt[b]-1)//2)*cnt[a] % MOD
for a in cnt.keys():
if 3*a == target and cnt[a]>=3:
res += (cnt[a]*(cnt[a]-1)*(cnt[a]-2))//6
return res%MOD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment