From b6b5db00b9285753515c000feac875ef29f74738 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 21 Oct 2024 19:02:39 +0800 Subject: [PATCH 1/3] feat: add solution to lc problem: No.3328 (#3656) No.3328.Find Cities in Each State II --- .prettierignore | 1 + .../README.md | 193 ++++++++++++++++++ .../README_EN.md | 193 ++++++++++++++++++ .../Solution.py | 25 +++ .../Solution.sql | 13 ++ solution/DATABASE_README.md | 1 + solution/DATABASE_README_EN.md | 1 + solution/README.md | 1 + solution/README_EN.md | 1 + 9 files changed, 429 insertions(+) create mode 100644 solution/3300-3399/3328.Find Cities in Each State II/README.md create mode 100644 solution/3300-3399/3328.Find Cities in Each State II/README_EN.md create mode 100644 solution/3300-3399/3328.Find Cities in Each State II/Solution.py create mode 100644 solution/3300-3399/3328.Find Cities in Each State II/Solution.sql diff --git a/.prettierignore b/.prettierignore index 05bf32e478051..13715bc116db8 100644 --- a/.prettierignore +++ b/.prettierignore @@ -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 diff --git a/solution/3300-3399/3328.Find Cities in Each State II/README.md b/solution/3300-3399/3328.Find Cities in Each State II/README.md new file mode 100644 index 0000000000000..7ff0db455e066 --- /dev/null +++ b/solution/3300-3399/3328.Find Cities in Each State II/README.md @@ -0,0 +1,193 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md +tags: + - 数据库 +--- + + + +# [3328. Find Cities in Each State II 🔒](https://leetcode.cn/problems/find-cities-in-each-state-ii) + +[English Version](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md) + +## 题目描述 + + + +

Table: cities

+ +
++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| state       | varchar |
+| city        | varchar |
++-------------+---------+
+(state, city) is the combination of columns with unique values for this table.
+Each row of this table contains the state name and the city name within that state.
+
+ +

Write a solution to find all the cities in each state and analyze them based on the following requirements:

+ + + +

Return the result table ordered by the count of matching-letter cities in descending order and then by state name in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

cities table:

+ +
++--------------+---------------+
+| state        | city          |
++--------------+---------------+
+| New York     | New York City |
+| New York     | Newark        |
+| New York     | Buffalo       |
+| New York     | Rochester     |
+| California   | San Francisco |
+| California   | Sacramento    |
+| California   | San Diego     |
+| California   | Los Angeles   |
+| Texas        | Tyler         |
+| Texas        | Temple        |
+| Texas        | Taylor        |
+| Texas        | Dallas        |
+| Pennsylvania | Philadelphia  |
+| Pennsylvania | Pittsburgh    |
+| Pennsylvania | Pottstown     |
++--------------+---------------+
+
+ +

Output:

+ +
++-------------+-------------------------------------------+-----------------------+
+| state       | cities                                    | matching_letter_count |
++-------------+-------------------------------------------+-----------------------+
+| Pennsylvania| Philadelphia, Pittsburgh, Pottstown       | 3                     |
+| Texas       | Dallas, Taylor, Temple, Tyler             | 2                     |
+| New York    | Buffalo, Newark, New York City, Rochester | 2                     |
++-------------+-------------------------------------------+-----------------------+
+
+ +

Explanation:

+ + + +

Note:

+ + +
+ + + +## 解法 + + + +### 方法一:分组聚合 + 过滤 + +我们可以将 `cities` 表按照 `state` 字段进行分组聚合,然后对每个分组进行过滤,筛选出满足条件的分组。 + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + state, + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') AS cities, + COUNT( + CASE + WHEN LEFT(city, 1) = LEFT(state, 1) THEN 1 + END + ) AS matching_letter_count +FROM cities +GROUP BY 1 +HAVING COUNT(city) >= 3 AND matching_letter_count > 0 +ORDER BY 3 DESC, 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def state_city_analysis(cities: pd.DataFrame) -> pd.DataFrame: + cities["matching_letter"] = cities["city"].str[0] == cities["state"].str[0] + + result = ( + cities.groupby("state") + .agg( + cities=("city", lambda x: ", ".join(sorted(x))), + matching_letter_count=("matching_letter", "sum"), + city_count=("city", "count"), + ) + .reset_index() + ) + + result = result[(result["city_count"] >= 3) & (result["matching_letter_count"] > 0)] + + result = result.sort_values( + by=["matching_letter_count", "state"], ascending=[False, True] + ) + + result = result.drop(columns=["city_count"]) + + return result +``` + + + + + + diff --git a/solution/3300-3399/3328.Find Cities in Each State II/README_EN.md b/solution/3300-3399/3328.Find Cities in Each State II/README_EN.md new file mode 100644 index 0000000000000..f1eab9077e28c --- /dev/null +++ b/solution/3300-3399/3328.Find Cities in Each State II/README_EN.md @@ -0,0 +1,193 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md +tags: + - Database +--- + + + +# [3328. Find Cities in Each State II 🔒](https://leetcode.com/problems/find-cities-in-each-state-ii) + +[中文文档](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) + +## Description + + + +

Table: cities

+ +
++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| state       | varchar |
+| city        | varchar |
++-------------+---------+
+(state, city) is the combination of columns with unique values for this table.
+Each row of this table contains the state name and the city name within that state.
+
+ +

Write a solution to find all the cities in each state and analyze them based on the following requirements:

+ + + +

Return the result table ordered by the count of matching-letter cities in descending order and then by state name in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

cities table:

+ +
++--------------+---------------+
+| state        | city          |
++--------------+---------------+
+| New York     | New York City |
+| New York     | Newark        |
+| New York     | Buffalo       |
+| New York     | Rochester     |
+| California   | San Francisco |
+| California   | Sacramento    |
+| California   | San Diego     |
+| California   | Los Angeles   |
+| Texas        | Tyler         |
+| Texas        | Temple        |
+| Texas        | Taylor        |
+| Texas        | Dallas        |
+| Pennsylvania | Philadelphia  |
+| Pennsylvania | Pittsburgh    |
+| Pennsylvania | Pottstown     |
++--------------+---------------+
+
+ +

Output:

+ +
++-------------+-------------------------------------------+-----------------------+
+| state       | cities                                    | matching_letter_count |
++-------------+-------------------------------------------+-----------------------+
+| Pennsylvania| Philadelphia, Pittsburgh, Pottstown       | 3                     |
+| Texas       | Dallas, Taylor, Temple, Tyler             | 2                     |
+| New York    | Buffalo, Newark, New York City, Rochester | 2                     |
++-------------+-------------------------------------------+-----------------------+
+
+ +

Explanation:

+ + + +

Note:

+ + +
+ + + +## Solutions + + + +### Solution 1: Group Aggregation + Filtering + +We can group the `cities` table by the `state` field, then apply filtering on each group to retain only the groups that meet the specified conditions. + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + state, + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') AS cities, + COUNT( + CASE + WHEN LEFT(city, 1) = LEFT(state, 1) THEN 1 + END + ) AS matching_letter_count +FROM cities +GROUP BY 1 +HAVING COUNT(city) >= 3 AND matching_letter_count > 0 +ORDER BY 3 DESC, 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def state_city_analysis(cities: pd.DataFrame) -> pd.DataFrame: + cities["matching_letter"] = cities["city"].str[0] == cities["state"].str[0] + + result = ( + cities.groupby("state") + .agg( + cities=("city", lambda x: ", ".join(sorted(x))), + matching_letter_count=("matching_letter", "sum"), + city_count=("city", "count"), + ) + .reset_index() + ) + + result = result[(result["city_count"] >= 3) & (result["matching_letter_count"] > 0)] + + result = result.sort_values( + by=["matching_letter_count", "state"], ascending=[False, True] + ) + + result = result.drop(columns=["city_count"]) + + return result +``` + + + + + + diff --git a/solution/3300-3399/3328.Find Cities in Each State II/Solution.py b/solution/3300-3399/3328.Find Cities in Each State II/Solution.py new file mode 100644 index 0000000000000..3c33145d67547 --- /dev/null +++ b/solution/3300-3399/3328.Find Cities in Each State II/Solution.py @@ -0,0 +1,25 @@ +import pandas as pd + + +def state_city_analysis(cities: pd.DataFrame) -> pd.DataFrame: + cities["matching_letter"] = cities["city"].str[0] == cities["state"].str[0] + + result = ( + cities.groupby("state") + .agg( + cities=("city", lambda x: ", ".join(sorted(x))), + matching_letter_count=("matching_letter", "sum"), + city_count=("city", "count"), + ) + .reset_index() + ) + + result = result[(result["city_count"] >= 3) & (result["matching_letter_count"] > 0)] + + result = result.sort_values( + by=["matching_letter_count", "state"], ascending=[False, True] + ) + + result = result.drop(columns=["city_count"]) + + return result diff --git a/solution/3300-3399/3328.Find Cities in Each State II/Solution.sql b/solution/3300-3399/3328.Find Cities in Each State II/Solution.sql new file mode 100644 index 0000000000000..ce985e262d394 --- /dev/null +++ b/solution/3300-3399/3328.Find Cities in Each State II/Solution.sql @@ -0,0 +1,13 @@ +# Write your MySQL query statement below +SELECT + state, + GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') AS cities, + COUNT( + CASE + WHEN LEFT(city, 1) = LEFT(state, 1) THEN 1 + END + ) AS matching_letter_count +FROM cities +GROUP BY 1 +HAVING COUNT(city) >= 3 AND matching_letter_count > 0 +ORDER BY 3 DESC, 1; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index c396c38a3b2ad..c90cbda654bbf 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -298,6 +298,7 @@ | 3293 | [计算产品最终价格](/solution/3200-3299/3293.Calculate%20Product%20Final%20Price/README.md) | `数据库` | 中等 | 🔒 | | 3308 | [Find Top Performing Driver](/solution/3300-3399/3308.Find%20Top%20Performing%20Driver/README.md) | `数据库` | 中等 | 🔒 | | 3322 | [英超积分榜排名 III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README.md) | `数据库` | 中等 | 🔒 | +| 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 0ad562b350bea..46112987e16f6 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -296,6 +296,7 @@ Press Control + F(or Command + F on | 3293 | [Calculate Product Final Price](/solution/3200-3299/3293.Calculate%20Product%20Final%20Price/README_EN.md) | `Database` | Medium | 🔒 | | 3308 | [Find Top Performing Driver](/solution/3300-3399/3308.Find%20Top%20Performing%20Driver/README_EN.md) | `Database` | Medium | 🔒 | | 3322 | [Premier League Table Ranking III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README_EN.md) | `Database` | Medium | 🔒 | +| 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index fff63e2896af9..46544026be341 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3338,6 +3338,7 @@ | 3325 | [字符至少出现 K 次的子字符串 I](/solution/3300-3399/3325.Count%20Substrings%20With%20K-Frequency%20Characters%20I/README.md) | | 中等 | 第 420 场周赛 | | 3326 | [使数组非递减的最少除法操作次数](/solution/3300-3399/3326.Minimum%20Division%20Operations%20to%20Make%20Array%20Non%20Decreasing/README.md) | | 中等 | 第 420 场周赛 | | 3327 | [判断 DFS 字符串是否是回文串](/solution/3300-3399/3327.Check%20if%20DFS%20Strings%20Are%20Palindromes/README.md) | | 困难 | 第 420 场周赛 | +| 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index b07d1b10a7ede..cbe04361f3fea 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3336,6 +3336,7 @@ Press Control + F(or Command + F on | 3325 | [Count Substrings With K-Frequency Characters I](/solution/3300-3399/3325.Count%20Substrings%20With%20K-Frequency%20Characters%20I/README_EN.md) | | Medium | Weekly Contest 420 | | 3326 | [Minimum Division Operations to Make Array Non Decreasing](/solution/3300-3399/3326.Minimum%20Division%20Operations%20to%20Make%20Array%20Non%20Decreasing/README_EN.md) | | Medium | Weekly Contest 420 | | 3327 | [Check if DFS Strings Are Palindromes](/solution/3300-3399/3327.Check%20if%20DFS%20Strings%20Are%20Palindromes/README_EN.md) | | Hard | Weekly Contest 420 | +| 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md) | | Medium | 🔒 | ## Copyright From a68d6953ff8b709045c2bf3a676e52f12b950749 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 21 Oct 2024 19:58:01 +0800 Subject: [PATCH 2/3] feat: add solutions to lc problem: No.3326 (#3657) No.3326.Minimum Division Operations to Make Array Non Decreasing --- .../README.md | 147 +++++++++++++++++- .../README_EN.md | 147 +++++++++++++++++- .../Solution.cpp | 32 ++++ .../Solution.go | 28 ++++ .../Solution.java | 26 ++++ .../Solution.py | 19 +++ .../Solution.ts | 23 +++ 7 files changed, 414 insertions(+), 8 deletions(-) create mode 100644 solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.cpp create mode 100644 solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.go create mode 100644 solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.java create mode 100644 solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.py create mode 100644 solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.ts diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README.md b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README.md index 4beee1bfb0cca..8a0f90769365c 100644 --- a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README.md +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README.md @@ -70,32 +70,171 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3326.Mi -### 方法一 +### 方法一:预处理 + 贪心 + +根据题目描述, + +如果整数 $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)$。 #### 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& 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; +} ``` diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README_EN.md b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README_EN.md index 58878e2b8e546..f9e54053dd3f3 100644 --- a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README_EN.md +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/README_EN.md @@ -67,32 +67,171 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3326.Mi -### 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)$. #### 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& 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; +} ``` diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.cpp b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.cpp new file mode 100644 index 0000000000000..95c6dbc37b71e --- /dev/null +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.cpp @@ -0,0 +1,32 @@ +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& 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; + } +}; diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.go b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.go new file mode 100644 index 0000000000000..ad8b10ca0ad84 --- /dev/null +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.go @@ -0,0 +1,28 @@ +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 +} diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.java b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.java new file mode 100644 index 0000000000000..77a46ec0cfd30 --- /dev/null +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.java @@ -0,0 +1,26 @@ +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; + } +} diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.py b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.py new file mode 100644 index 0000000000000..30984f505d427 --- /dev/null +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.py @@ -0,0 +1,19 @@ +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 diff --git a/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.ts b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.ts new file mode 100644 index 0000000000000..4e32b89fc8a14 --- /dev/null +++ b/solution/3300-3399/3326.Minimum Division Operations to Make Array Non Decreasing/Solution.ts @@ -0,0 +1,23 @@ +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; +} From 36904ac502d7e24fa7a4c5033662191a1b41b7a9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 21 Oct 2024 19:59:11 +0800 Subject: [PATCH 3/3] chore: update star data (#3658) --- README.md | 12 +++++++----- README_EN.md | 11 +++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8431bfcb6e5fe..fb03f8b48b1f5 100644 --- a/README.md +++ b/README.md @@ -199,13 +199,15 @@ https://doocs.github.io/leetcode ## Stars 趋势 - - - - - Stargazers over time + + + + Star Geographical Distribution of doocs/leetcode + + + ## 贡献者 感谢以下所有朋友对本项目的贡献! diff --git a/README_EN.md b/README_EN.md index 7334ac3182bcf..d81be5f48286a 100644 --- a/README_EN.md +++ b/README_EN.md @@ -191,12 +191,15 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http ## Stargazers over time - - - - Stargazers over time + + + + Star Geographical Distribution of doocs/leetcode + + + ## Our Top Contributors This project exists thanks to all the people who contribute.