-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
40 lines (40 loc) · 1.11 KB
/
Solution.java
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
class Solution {
public int findValidSplit(int[] nums) {
Map<Integer, Integer> first = new HashMap<>();
int n = nums.length;
int[] last = new int[n];
for (int i = 0; i < n; ++i) {
last[i] = i;
}
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.containsKey(j)) {
last[first.get(j)] = i;
} else {
first.put(j, i);
}
while (x % j == 0) {
x /= j;
}
}
}
if (x > 1) {
if (first.containsKey(x)) {
last[first.get(x)] = i;
} else {
first.put(x, i);
}
}
}
int mx = last[0];
for (int i = 0; i < n; ++i) {
if (mx < i) {
return mx;
}
mx = Math.max(mx, last[i]);
}
return -1;
}
}