-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcount-special-triplets.py
More file actions
51 lines (44 loc) · 1.16 KB
/
count-special-triplets.py
File metadata and controls
51 lines (44 loc) · 1.16 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
50
51
# Time: O(n)
# Space: O(n)
import collections
# dp
class Solution(object):
def specialTriplets(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
MOD = 10**9+7
result = 0
dp = [collections.defaultdict(int) for _ in xrange(2)]
for x in nums:
if x%2 == 0 and x//2 in dp[1]:
result = (result+dp[1][x//2])%MOD
if 2*x in dp[0]:
dp[1][x] = (dp[1][x]+dp[0][2*x])%MOD
dp[0][x] = (dp[0][x]+1)%MOD
return result
# Time: O(n)
# Space: O(n)
import collections
# freq table
class Solution2(object):
def specialTriplets(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
MOD = 10**9+7
result = 0
right = collections.defaultdict(int)
for x in nums:
right[x] += 1
left = collections.defaultdict(int)
for x in nums:
right[x] -= 1
if not right[x]:
del right[x]
if 2*x in left and 2*x in right:
result = (result+left[2*x]*right[2*x])%MOD
left[x] += 1
return result