forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split-the-array-to-make-coprime-products.cpp
49 lines (47 loc) · 1.29 KB
/
split-the-array-to-make-coprime-products.cpp
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
// Time: O(n * sqrt(r)), r = max(nums)
// Space: O(sqrt(r))
// number theory
class Solution {
public:
int findValidSplit(vector<int>& nums) {
const auto& factorize = [](int x) {
vector<pair<int, int>> result;
for (int d = 2; d * d <= x; d += (d == 2) ? 1: 2) {
int e = 0;
for (; x % d == 0; ++e) {
x /= d;
}
if (e) {
result.emplace_back(d, e);
}
}
if (x > 1) {
result.emplace_back(x, 1);
}
return result;
};
unordered_map<int, int> right;
for (const auto& x : nums) {
for (const auto& [p, c] : factorize(x)) {
right[p] += c;
}
}
unordered_map<int, int> left;
for (int i = 0, cnt = 0; i < size(nums) - 1; ++i) {
for (const auto& [p, c] : factorize(nums[i])) {
if (!left[p]) {
++cnt;
}
left[p] += c;
right[p] -= c;
if (!right[p]) {
--cnt;
}
if (!cnt) {
return i;
}
}
}
return -1;
}
};