Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #73

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
pull merged 5 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions Math/204.Count-Primes/204.Count Primes.cpp
View file Open in desktop

This file was deleted.

19 changes: 19 additions & 0 deletions Math/204.Count-Primes/204.Count-Primes.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int countPrimes(int n)
{
vector<int>isPrime(n, true);
int count = 0;
for (int i=2; i<n; i++)
{
if (isPrime[i]==false) continue;
count++;

if (i < sqrt(n))
for (int j=2*i; j<n; j+=i)
isPrime[j] = false;
}

return count;
}
};
14 changes: 1 addition & 13 deletions Math/204.Count-Primes/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
### 204.Count-Primes

用倍数筛除法去除所有已知质数的倍数。
```cpp
vector<bool>q(n,true);
for (x=2; x<=sqrt(n); x++)
{
if (q[x]==false) continue;
for (int i=2; x*i<n; i++)
q[x*i]=false;
}
```
然后检查从2到n-1里仍然标记为true的元素数目。

注意,x的判断范围是从2到sqrt(n)即可,不需要遍历到n。
埃拉托斯特尼筛法,简称埃氏筛,是一种由希腊数学家埃拉托斯特尼所提出的一种简单检定素数的算法。 要得到自然数n以内的全部素数,必须把不大于根号n的所有素数的倍数剔除,剩下的就是素数。

埃氏筛的时间复杂度是O(NloglogN)

Expand Down
12 changes: 12 additions & 0 deletions Math/2448.Minimum-Cost-to-Make-Array-Equal/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### 2448.Minimum-Cost-to-Make-Array-Equal

#### 解法1:
如果本题的所有cost[i]都是1的话,我们发现这其实就是`296.Best-Meeting-Point`。我们应该对这个结论很熟悉:对于一维序列{xi},我们想找一个位置p使得`sum{abs(xi-p)}`最小化,那么p的位置一定就是{xi}的中位数。

对于本题而言,我们想找一个位置p使得`sum{abs(xi-p)*cost[i]}`最小化。这其实可以卡成将每个xi复制cost[i]份,从而得到一个长度为totalCost的序列,然后回归到上述的问题。显然,我们想要找的位置就是新序列的中位数。我们只需要每查看一个xi,就增加`count+=cost[i]`,直至发现count恰好超过了totalCost的一半时候为止,这个xi就是best equal value.


#### 解法2:
对于这类题目,我们通常会有一个大胆的猜测,最优解所对应的"best equal value"一定是nums里的某一个元素。为什么呢?假设这个best equal value是x,位于排序后的某两个元素之间(记做nums[i]和nums[j]),那么我们可以论证还会有比x更优的解。假如我们将equal value设为x+1,那么对于右边的元素,总共会减少costSum[j:n-1],对于左边的元素,总共会增加costSum[0:i],即`delta1 = costSum[0:i]-costSum[j:n-1]`. 假如我们将equal value设为x-1,类似的会有`delta2 = -costSum[0:i]+costSum[j:n-1]`。通常情况下delta1和delta2必然是一正一负,这就意味着必然有一个方向能够得到更优的策略(即x+1或者x-1)。这个方向的移动可以持续,直至best equal value设置在了nums[i]或者nums[j]上。

有了这个想法,我们只需要遍历一遍每个nums[i],假想它是equal value的话,cost是多少。cost包括了左边元素的提升costLeft,以及右边元素的下降costRight。我们从左往右遍历的时候,costLeft都是可以一路累加的,即`costLeft[i] = costLeft[i-1]+costSum[i:i-1]*(nums[i]-nums[i-1])`. 同理我们从右往左走一遍,求得所有的costRight[i]。最终我们挑选最小的`costLeft[i]+costRight[i]`.

AltStyle によって変換されたページ (->オリジナル) /