forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
split-the-array-to-make-coprime-products.py
45 lines (41 loc) · 1.13 KB
/
split-the-array-to-make-coprime-products.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
# Time: O(n * sqrt(r)), r = max(nums)
# Space: O(sqrt(r))
import collections
# number theory
class Solution(object):
def findValidSplit(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def factorize(x):
result = []
d = 2
while d*d <= x:
e = 0
while x%d == 0:
x //= d
e += 1
if e:
result.append([d, e])
d += 1 if d == 2 else 2
if x > 1:
result.append([x, 1])
return result
right = collections.Counter()
for x in reversed(nums):
for p, c in factorize(x):
right[p] += c
left = collections.Counter()
cnt = 0
for i in xrange(len(nums)-1):
for p, c in factorize(nums[i]):
if not left[p]:
cnt += 1
left[p] += c
right[p] -= c
if not right[p]:
cnt -= 1
if not cnt:
return i
return -1