-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3629.cpp
More file actions
66 lines (63 loc) · 2.08 KB
/
3629.cpp
File metadata and controls
66 lines (63 loc) · 2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public:
int minJumps(vector<int>& nums) {
int n = nums.size();
int maxVal = *max_element(nums.begin(), nums.end());
vector<int> num2minPrime(maxVal + 1, 0);
for (int i = 0; i <= maxVal; ++i) num2minPrime[i] = i;
for (int i = 2; i * i <= maxVal; ++i) {
if (num2minPrime[i] == i) {
for (int j = i * i; j <= maxVal; j += i) {
if (num2minPrime[j] == j) {
num2minPrime[j] = i;
}
}
}
}
unordered_map<int, vector<int>> prime2pos;
for (int i = 0; i < n; ++i) {
int x = nums[i];
while (x > 1) {
int prime = num2minPrime[x];
prime2pos[prime].push_back(i);
while (x % prime == 0) {
x /= prime;
}
}
}
int count = 0;
vector<bool> visited(n, false);
unordered_set<int> visitedPrime;
queue<int> q;
q.push(0);
visited[0] = true;
while (!q.empty()) {
int m = q.size();
while (m--) {
int node = q.front();
if (node == n - 1) return count;
q.pop();
if (node - 1 >= 0 && !visited[node - 1]) {
q.push(node - 1);
visited[node - 1] = true;
}
if (node + 1 < n && !visited[node + 1]) {
q.push(node + 1);
visited[node + 1] = true;
}
if (prime2pos.count(nums[node])) {
if (!visitedPrime.count(nums[node])) {
visitedPrime.insert(nums[node]);
for (auto& pos : prime2pos[nums[node]]) {
if (visited[pos]) continue;
visited[pos] = true;
q.push(pos);
}
}
}
}
count++;
}
return -1;
}
};