Last active
December 11, 2019 17:01
-
-
Save liketheflower/0e9bb48a3ab50a07026b55f64846ff72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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