Skip to content

Commit 5475ec6

Browse files
authored
Create number-of-single-divisor-triplets.py
1 parent 089f247 commit 5475ec6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(d^3), d is the number of distinct nums
2+
# Space: O(d)
3+
4+
import collections
5+
import itertools
6+
7+
# combinatorics
8+
class Solution(object):
9+
def singleDivisorTriplet(self, nums):
10+
"""
11+
:type nums: List[int]
12+
:rtype: int
13+
"""
14+
def check(a, b, c):
15+
return sum((a+b+c)%x == 0 for x in (a, b, c)) == 1
16+
17+
cnt = collections.Counter(nums)
18+
return 6*(sum(cnt[a]*cnt[b]*cnt[c] for a, b, c in itertools.combinations(cnt.keys(), 3) if check(a, b, c)) +
19+
sum(cnt[a]*(cnt[a]-1)//2*cnt[b] for a, b in itertools.permutations(cnt.keys(), 2) if check(a, a, b)))

0 commit comments

Comments
 (0)