-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
39 lines (39 loc) · 1.04 KB
/
Solution.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
class Solution {
public:
int findValidSplit(vector<int>& nums) {
unordered_map<int, int> first;
int n = nums.size();
vector<int> last(n);
iota(last.begin(), last.end(), 0);
for (int i = 0; i < n; ++i) {
int x = nums[i];
for (int j = 2; j <= x / j; ++j) {
if (x % j == 0) {
if (first.count(j)) {
last[first[j]] = i;
} else {
first[j] = i;
}
while (x % j == 0) {
x /= j;
}
}
}
if (x > 1) {
if (first.count(x)) {
last[first[x]] = i;
} else {
first[x] = i;
}
}
}
int mx = last[0];
for (int i = 0; i < n; ++i) {
if (mx < i) {
return mx;
}
mx = max(mx, last[i]);
}
return -1;
}
};