forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
...h-End-via-Prime-Teleportation/3629.Minimum-Jumps-to-Reach-End-via-Prime-Teleportation.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class Solution { | ||
public: | ||
int minJumps(vector<int>& nums) { | ||
int MAXA = *max_element(nums.begin(), nums.end()); | ||
vector<int>spf(MAXA+1, 0); | ||
for (int i=2; i<=MAXA;i++) { | ||
if (spf[i]) continue; | ||
for (int j=i; j<=MAXA; j+=i) | ||
if (!spf[j]) spf[j]=i; | ||
} | ||
|
||
vector<vector<int>>prime_to_idx(MAXA+1); | ||
int n = nums.size(); | ||
for (int i=0; i<n; i++) { | ||
int x = nums[i]; | ||
while (x>1) { | ||
int p = spf[x]; | ||
prime_to_idx[p].push_back(i); | ||
while (x%p==0) x/=p; | ||
} | ||
} | ||
|
||
const int INF = 1e9; | ||
vector<int>dist(n, INF); | ||
vector<int>used_prime(MAXA+1, 0); | ||
deque<int>q; | ||
dist[0] = 0; | ||
q.push_back(0); | ||
|
||
while (!q.empty()) { | ||
int i = q.front(); | ||
q.pop_front(); | ||
int d = dist[i]; | ||
if (i==n-1) | ||
return d; | ||
|
||
if (i+1<n && dist[i+1]==INF) { | ||
dist[i+1] = d+1; | ||
q.push_back(i+1); | ||
} | ||
if (i-1>=0 && dist[i-1]==INF) { | ||
dist[i-1] = d+1; | ||
q.push_back(i-1); | ||
} | ||
int x = nums[i]; | ||
if (x>1 && spf[x]==x) { | ||
if (used_prime[x]) continue; | ||
used_prime[x] = 1; | ||
for (int j: prime_to_idx[x]) { | ||
if (dist[j]==INF) { | ||
dist[j] = d+1; | ||
q.push_back(j); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
BFS/3629.Minimum-Jumps-to-Reach-End-via-Prime-Teleportation/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### 3629.Minimum-Jumps-to-Reach-End-via-Prime-Teleportation | ||
|
||
很容易判断,总体框架必然是BFS。 | ||
|
||
此题额外要求预处理nums里的每个质数与其倍数之间的映射关系,存入prime_to_idx中。一个高效的做法是在用埃氏筛判定1到M内的所有质数时,顺便记录下每个自然数的最小质因数(spf)。这样就方便我们对于nums的每个元素x做快速的质因数分解(不断去除以spf[x]),从而建立起它的所有质因子p到x的映射集合。 | ||
|
||
BFS的写法比较常规。从队列里弹出index=i后,考察是否需要将`i+1`, `i-1`以及`prime_to_idx[nums[i]]`(仅当nums[i]是质数时)放入队列。注意对于已经处理过的质数需要略过。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.