Skip to content

[pull] main from doocs:main #197

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 3 commits into from
Oct 22, 2024
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ node_modules/
/solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql
/solution/3100-3199/3150.Invalid Tweets II/Solution.sql
/solution/3100-3199/3198.Find Cities in Each State/Solution.sql
/solution/3300-3399/3328.Find Cities in Each State II/Solution.sql
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ https://doocs.github.io/leetcode

## Stars 趋势

<!-- 使用 https://starchart.cc/ 自动刷新 stars 数据,若有问题,可以使用 GitHub Action: starcharts.yml -->
<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a> -->

<!-- [![Star History Chart](https://api.star-history.com/svg?repos=doocs/leetcode&type=Date)](https://star-history.com/#doocs/leetcode) -->

<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a>

<a href="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map?repo_id=149001365&activity=stars" target="_blank" style="display: block" align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map/thumbnail.png?repo_id=149001365&activity=stars&image_size=auto&color_scheme=dark" width="721" height="auto">
<img alt="Star Geographical Distribution of doocs/leetcode" src="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map/thumbnail.png?repo_id=149001365&activity=stars&image_size=auto&color_scheme=light" width="721" height="auto">
</picture>
</a>

## 贡献者

感谢以下所有朋友对本项目的贡献!
Expand Down
11 changes: 7 additions & 4 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,15 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http

## Stargazers over time

<!-- <a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="https://starchart.cc/doocs/leetcode.svg" alt="Stargazers over time" /></a> -->

<!-- [![Star History Chart](https://api.star-history.com/svg?repos=doocs/leetcode&type=Date)](https://star-history.com/#doocs/leetcode) -->

<a href="https://github.com/doocs/leetcode/stargazers" target="_blank"><img src="./images/starcharts.svg" alt="Stargazers over time" /></a>

<a href="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map?repo_id=149001365&activity=stars" target="_blank" style="display: block" align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map/thumbnail.png?repo_id=149001365&activity=stars&image_size=auto&color_scheme=dark" width="721" height="auto">
<img alt="Star Geographical Distribution of doocs/leetcode" src="https://next.ossinsight.io/widgets/official/analyze-repo-stars-map/thumbnail.png?repo_id=149001365&activity=stars&image_size=auto&color_scheme=light" width="721" height="auto">
</picture>
</a>

## Our Top Contributors

This project exists thanks to all the people who contribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,171 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3326.Mi

<!-- solution:start -->

### 方法一
### 方法一:预处理 + 贪心

根据题目描述,

如果整数 $x$ 是质数,那么它的最大真因数是 $1$,那么 $x / 1 = x$,即 $x$ 不能再被除了;

如果整数 $x$ 不是质数,我们假设 $x$ 的最大真因数为 $y$,那么 $x / y$ 一定是质数,因此,我们寻找最小质数 $\textit{lpf}[x]$,使得 $x \bmod \textit{lpf}[x] = 0$,使得 $x$ 变成 $\textit{lpf}[x]$,此时无法再被除了。

因此,我们可以预处理出 $1$ 到 $10^6$ 的每个整数的最小质因数,然后从右往左遍历数组,如果当前元素大于下一个元素,我们将当前元素变为它的最小质因数,如果当前元素变为它的最小质因数后,仍然大于下一个元素,说明无法将数组变成非递减的,返回 $-1$。否则,操作次数加一。继续遍历,直到遍历完整个数组。

预处理的时间复杂度为 $O(M \times \log \log M)$,其中 $M = 10^6$,遍历数组的时间复杂度为 $O(n)$,其中 $n$ 为数组的长度。空间复杂度为 $O(M)$。

<!-- tabs:start -->

#### Python3

```python

mx = 10**6 + 1
lpf = [0] * (mx + 1)
for i in range(2, mx + 1):
if lpf[i] == 0:
for j in range(i, mx + 1, i):
if lpf[j] == 0:
lpf[j] = i


class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
for i in range(len(nums) - 2, -1, -1):
if nums[i] > nums[i + 1]:
nums[i] = lpf[nums[i]]
if nums[i] > nums[i + 1]:
return -1
ans += 1
return ans
```

#### Java

```java

class Solution {
private static final int MX = (int) 1e6 + 1;
private static final int[] LPF = new int[MX + 1];
static {
for (int i = 2; i <= MX; ++i) {
for (int j = i; j <= MX; j += i) {
if (LPF[j] == 0) {
LPF[j] = i;
}
}
}
}
public int minOperations(int[] nums) {
int ans = 0;
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1]) {
nums[i] = LPF[nums[i]];
if (nums[i] > nums[i + 1]) {
return -1;
}
ans++;
}
}
return ans;
}
}
```

#### C++

```cpp

const int MX = 1e6;
int LPF[MX + 1];

auto init = [] {
for (int i = 2; i <= MX; i++) {
if (LPF[i] == 0) {
for (int j = i; j <= MX; j += i) {
if (LPF[j] == 0) {
LPF[j] = i;
}
}
}
}
return 0;
}();

class Solution {
public:
int minOperations(vector<int>& nums) {
int ans = 0;
for (int i = nums.size() - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1]) {
nums[i] = LPF[nums[i]];
if (nums[i] > nums[i + 1]) {
return -1;
}
ans++;
}
}
return ans;
}
};
```

#### Go

```go
const mx int = 1e6

var lpf = [mx + 1]int{}

func init() {
for i := 2; i <= mx; i++ {
if lpf[i] == 0 {
for j := i; j <= mx; j += i {
if lpf[j] == 0 {
lpf[j] = i
}
}
}
}
}

func minOperations(nums []int) (ans int) {
for i := len(nums) - 2; i >= 0; i-- {
if nums[i] > nums[i+1] {
nums[i] = lpf[nums[i]]
if nums[i] > nums[i+1] {
return -1
}
ans++
}
}
return
}
```

#### TypeScript

```ts
const mx = 10 ** 6;
const lpf = Array(mx + 1).fill(0);
for (let i = 2; i <= mx; ++i) {
for (let j = i; j <= mx; j += i) {
if (lpf[j] === 0) {
lpf[j] = i;
}
}
}

function minOperations(nums: number[]): number {
let ans = 0;
for (let i = nums.length - 2; ~i; --i) {
if (nums[i] > nums[i + 1]) {
nums[i] = lpf[nums[i]];
if (nums[i] > nums[i + 1]) {
return -1;
}
++ans;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,171 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3326.Mi

<!-- solution:start -->

### Solution 1
### Solution 1: Preprocessing + Greedy

According to the problem description,

If an integer $x$ is a prime number, then its largest proper divisor is $1$, so $x / 1 = x$, meaning $x$ cannot be divided further.

If an integer $x$ is not a prime number, we assume the largest proper divisor of $x$ is $y$, then $x / y$ must be a prime number. Therefore, we find the smallest prime factor $\textit{lpf}[x]$ such that $x \bmod \textit{lpf}[x] = 0$, making $x$ become $\textit{lpf}[x]$, at which point it cannot be divided further.

Thus, we can preprocess the smallest prime factor for each integer from $1$ to $10^6$. Then, we traverse the array from right to left. If the current element is greater than the next element, we change the current element to its smallest prime factor. If after changing the current element to its smallest prime factor it is still greater than the next element, it means the array cannot be made non-decreasing, and we return $-1$. Otherwise, we increment the operation count by one. Continue traversing until the entire array is processed.

The time complexity for preprocessing is $O(M \times \log \log M)$, where $M = 10^6$. The time complexity for traversing the array is $O(n)$, where $n$ is the length of the array. The space complexity is $O(M)$.

<!-- tabs:start -->

#### Python3

```python

mx = 10**6 + 1
lpf = [0] * (mx + 1)
for i in range(2, mx + 1):
if lpf[i] == 0:
for j in range(i, mx + 1, i):
if lpf[j] == 0:
lpf[j] = i


class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
for i in range(len(nums) - 2, -1, -1):
if nums[i] > nums[i + 1]:
nums[i] = lpf[nums[i]]
if nums[i] > nums[i + 1]:
return -1
ans += 1
return ans
```

#### Java

```java

class Solution {
private static final int MX = (int) 1e6 + 1;
private static final int[] LPF = new int[MX + 1];
static {
for (int i = 2; i <= MX; ++i) {
for (int j = i; j <= MX; j += i) {
if (LPF[j] == 0) {
LPF[j] = i;
}
}
}
}
public int minOperations(int[] nums) {
int ans = 0;
for (int i = nums.length - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1]) {
nums[i] = LPF[nums[i]];
if (nums[i] > nums[i + 1]) {
return -1;
}
ans++;
}
}
return ans;
}
}
```

#### C++

```cpp

const int MX = 1e6;
int LPF[MX + 1];

auto init = [] {
for (int i = 2; i <= MX; i++) {
if (LPF[i] == 0) {
for (int j = i; j <= MX; j += i) {
if (LPF[j] == 0) {
LPF[j] = i;
}
}
}
}
return 0;
}();

class Solution {
public:
int minOperations(vector<int>& nums) {
int ans = 0;
for (int i = nums.size() - 2; i >= 0; i--) {
if (nums[i] > nums[i + 1]) {
nums[i] = LPF[nums[i]];
if (nums[i] > nums[i + 1]) {
return -1;
}
ans++;
}
}
return ans;
}
};
```

#### Go

```go
const mx int = 1e6

var lpf = [mx + 1]int{}

func init() {
for i := 2; i <= mx; i++ {
if lpf[i] == 0 {
for j := i; j <= mx; j += i {
if lpf[j] == 0 {
lpf[j] = i
}
}
}
}
}

func minOperations(nums []int) (ans int) {
for i := len(nums) - 2; i >= 0; i-- {
if nums[i] > nums[i+1] {
nums[i] = lpf[nums[i]]
if nums[i] > nums[i+1] {
return -1
}
ans++
}
}
return
}
```

#### TypeScript

```ts
const mx = 10 ** 6;
const lpf = Array(mx + 1).fill(0);
for (let i = 2; i <= mx; ++i) {
for (let j = i; j <= mx; j += i) {
if (lpf[j] === 0) {
lpf[j] = i;
}
}
}

function minOperations(nums: number[]): number {
let ans = 0;
for (let i = nums.length - 2; ~i; --i) {
if (nums[i] > nums[i + 1]) {
nums[i] = lpf[nums[i]];
if (nums[i] > nums[i + 1]) {
return -1;
}
++ans;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Loading