forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-divisible-triplet-sums.py
67 lines (56 loc) · 1.47 KB
/
number-of-divisible-triplet-sums.py
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
61
62
63
64
65
66
67
# Time: O(n^2)
# Space: O(n)
import collections
# freq table
class Solution(object):
def divisibleTripletCount(self, nums, d):
"""
:type nums: List[int]
:type d: int
:rtype: int
"""
result = 0
cnt = collections.Counter()
for i in xrange(len(nums)):
for j in xrange(i+1, len(nums)):
if (nums[i]+nums[j])%d in cnt:
result += cnt[(nums[i]+nums[j])%d]
cnt[-nums[i]%d] += 1
return result
# Time: O(n^2)
# Space: O(n^2)
import collections
# freq table
class Solution2(object):
def divisibleTripletCount(self, nums, d):
"""
:type nums: List[int]
:type d: int
:rtype: int
"""
result = 0
cnt = collections.Counter()
for i in xrange(len(nums)):
if nums[i]%d in cnt:
result += cnt[nums[i]%d]
for j in xrange(i):
cnt[-(nums[i]+nums[j])%d] += 1
return result
# Time: O(n^2)
# Space: O(n)
import collections
# freq table
class Solution3(object):
def divisibleTripletCount(self, nums, d):
"""
:type nums: List[int]
:type d: int
:rtype: int
"""
result = 0
for i in xrange(len(nums)):
cnt = collections.Counter()
for j in xrange(i+1, len(nums)):
result += cnt[nums[j]%d]
cnt[-(nums[i]+nums[j])%d] += 1
return result