-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2584.cpp
More file actions
38 lines (37 loc) · 1.08 KB
/
2584.cpp
File metadata and controls
38 lines (37 loc) · 1.08 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
class Solution {
public:
unordered_map<int, int> factor(int x) {
unordered_map<int, int> res;
for (int i = 2; i <= 1000; ++i) {
while (x % i == 0) {
x /= i;
res[i]++;
}
}
if (x != 1) res[x]++;
return res;
}
int findValidSplit(vector<int>& nums) {
int n = nums.size();
unordered_map<int, int> freqBack;
vector<unordered_map<int, int>> numsHash(n);
for (int i = 0; i < n; ++i) {
auto d = factor(nums[i]);
for (auto [f, c] : d) {
freqBack[f] += c;
}
numsHash[i] = d;
}
int dirty = 0;
auto ori = freqBack;
for (int i = 0; i <= n - 2; ++i) {
for (auto [f, c] : numsHash[i]) {
if (ori[f] == freqBack[f] && freqBack[f] != c) dirty++;
if (ori[f] != freqBack[f] && freqBack[f] == c) dirty--;
freqBack[f] -= c;
}
if (dirty == 0) return i;
}
return -1;
}
};