From 032e23bc6c4b31e40a3dc7001369f75f89f16f9b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 19 Jun 2024 13:04:34 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.0829 No.0829.Consecutive Numbers Sum --- .../0829.Consecutive Numbers Sum/README.md | 31 +++++++++++++--- .../0829.Consecutive Numbers Sum/README_EN.md | 35 +++++++++++++++++-- .../0829.Consecutive Numbers Sum/Solution.py | 2 +- .../0829.Consecutive Numbers Sum/Solution.ts | 10 ++++++ 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 solution/0800-0899/0829.Consecutive Numbers Sum/Solution.ts diff --git a/solution/0800-0899/0829.Consecutive Numbers Sum/README.md b/solution/0800-0899/0829.Consecutive Numbers Sum/README.md index 6a41f2529e51a..5f674cfe5eaa2 100644 --- a/solution/0800-0899/0829.Consecutive Numbers Sum/README.md +++ b/solution/0800-0899/0829.Consecutive Numbers Sum/README.md @@ -58,15 +58,21 @@ tags: ### 方法一:数学推导 -连续正整数构成一个等差数列($d=1$)。我们假设等差数列的第一项为 $a$,项数为 $k$,则 $n=(a+a+k-1)*k/2$,即 $n*2=(a*2+k-1)*k$ ①。 +连续正整数构成一个公差 $d = 1$ 的等差数列。我们假设等差数列的第一项为 $a$,项数为 $k$,那么 $n = (a + a + k - 1) \times k / 2$,即 $n \times 2 = (a \times 2 + k - 1) \times k$。这里我们可以得出 $k$ 一定能整除 $n \times 2$,并且 $(n \times 2) / k - k + 1$ 一定是偶数。 -由于是连续正整数, $a>=1$,所以 ① 可以化为 $n*2>=(k+1)*k$,即 $k*(k+1)<=n*2$ ②。 +由于 $a \geq 1$,所以 $n \times 2 = (a \times 2 + k - 1) \times k \geq k \times (k + 1)$。 -因此,$k$ 的范围需要满足 $k>=1$ 并且 $k*(k+1)<=n*2$。另外,我们从 ① 式可以发现,$k$ 必须能整除 $n*2$。 +综上,我们可以得出: -综上,我们枚举 $k$,累加满足条件的 $k$ 的个数即可。 +1. $k$ 一定能整除 $n \times 2$; +2. $k \times (k + 1) \leq n \times 2$; +3. $(n \times 2) / k - k + 1$ 一定是偶数。 -时间复杂度 $O(\sqrt{n})$。 +我们从 $k = 1$ 开始枚举,当 $k \times (k + 1) > n \times 2$ 时,我们可以结束枚举。在枚举的过程中,我们判断 $k$ 是否能整除 $n \times 2$,并且 $(n \times 2) / k - k + 1$ 是否是偶数,如果是则满足条件,答案加一。 + +枚举结束后,返回答案即可。 + +时间复杂度 $O(\sqrt{n})$,其中 $n$ 为给定的正整数。空间复杂度 $O(1)$。 @@ -135,6 +141,21 @@ func consecutiveNumbersSum(n int) int { } ``` +#### TypeScript + +```ts +function consecutiveNumbersSum(n: number): number { + let ans = 0; + n <<= 1; + for (let k = 1; k * (k + 1) <= n; ++k) { + if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) { + ++ans; + } + } + return ans; +} +``` + diff --git a/solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md b/solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md index 7531ec06aeb2c..44a89815dac70 100644 --- a/solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md +++ b/solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md @@ -57,7 +57,23 @@ tags: -### Solution 1 +### Solution 1: Mathematical Derivation + +Consecutive positive integers form an arithmetic sequence with a common difference $d = 1$. Let's assume the first term of the sequence is $a$, and the number of terms is $k$. Then, $n = (a + a + k - 1) \times k / 2$, which simplifies to $n \times 2 = (a \times 2 + k - 1) \times k$. From this, we can deduce that $k$ must divide $n \times 2$ evenly, and $(n \times 2) / k - k + 1$ must be an even number. + +Given that $a \geq 1$, it follows that $n \times 2 = (a \times 2 + k - 1) \times k \geq k \times (k + 1)$. + +In summary, we can conclude: + +1. $k$ must divide $n \times 2$ evenly; +2. $k \times (k + 1) \leq n \times 2$; +3. $(n \times 2) / k - k + 1$ must be an even number. + +We start enumerating from $k = 1$, and we can stop when $k \times (k + 1) > n \times 2$. During the enumeration, we check if $k$ divides $n \times 2$ evenly, and if $(n \times 2) / k - k + 1$ is an even number. If both conditions are met, it satisfies the criteria, and we increment the answer by one. + +After finishing the enumeration, we return the answer. + +The time complexity is $O(\sqrt{n})$, where $n$ is the given positive integer. The space complexity is $O(1)$. @@ -69,7 +85,7 @@ class Solution: n <<= 1 ans, k = 0, 1 while k * (k + 1) <= n: - if n % k == 0 and (n // k + 1 - k) % 2 == 0: + if n % k == 0 and (n // k - k + 1) % 2 == 0: ans += 1 k += 1 return ans @@ -126,6 +142,21 @@ func consecutiveNumbersSum(n int) int { } ``` +#### TypeScript + +```ts +function consecutiveNumbersSum(n: number): number { + let ans = 0; + n <<= 1; + for (let k = 1; k * (k + 1) <= n; ++k) { + if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) { + ++ans; + } + } + return ans; +} +``` + diff --git a/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.py b/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.py index b0dbe1960676a..fe343173266d7 100644 --- a/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.py +++ b/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.py @@ -3,7 +3,7 @@ def consecutiveNumbersSum(self, n: int) -> int: n <<= 1 ans, k = 0, 1 while k * (k + 1) <= n: - if n % k == 0 and (n // k + 1 - k) % 2 == 0: + if n % k == 0 and (n // k - k + 1) % 2 == 0: ans += 1 k += 1 return ans diff --git a/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.ts b/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.ts new file mode 100644 index 0000000000000..053f93127b0c4 --- /dev/null +++ b/solution/0800-0899/0829.Consecutive Numbers Sum/Solution.ts @@ -0,0 +1,10 @@ +function consecutiveNumbersSum(n: number): number { + let ans = 0; + n <<= 1; + for (let k = 1; k * (k + 1) <= n; ++k) { + if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) { + ++ans; + } + } + return ans; +} From ff48d0df7f99c135d67878e42ddc688ea534c380 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 19 Jun 2024 13:12:35 +0800 Subject: [PATCH 2/2] feat: update solutions to lc problem --- .../README.md | 71 ++++++++++--------- .../README_EN.md | 14 ++-- .../Solution.sql | 8 +-- solution/DATABASE_README.md | 2 +- solution/README.md | 2 +- 5 files changed, 49 insertions(+), 48 deletions(-) diff --git a/solution/3100-3199/3188.Find Top Scoring Students II/README.md b/solution/3100-3199/3188.Find Top Scoring Students II/README.md index f044524f1257b..750b6d123dce2 100644 --- a/solution/3100-3199/3188.Find Top Scoring Students II/README.md +++ b/solution/3100-3199/3188.Find Top Scoring Students II/README.md @@ -6,7 +6,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Fi -# [3188. Find Top Scoring Students II 🔒](https://leetcode.cn/problems/find-top-scoring-students-ii) +# [3188. 查找得分最高的学生 II 🔒](https://leetcode.cn/problems/find-top-scoring-students-ii) [English Version](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md) @@ -14,7 +14,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Fi -

Table: students

+

表:students

 +-------------+----------+
@@ -24,11 +24,11 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Fi
 | name        | varchar  |
 | major       | varchar  |
 +-------------+----------+
-student_id is the primary key for this table. 
-Each row contains the student ID, student name, and their major.
+student_id 是这张表的主键(有不同值的列的组合)。
+这张表的每一行包含学生 ID,学生姓名和他们的专业。
 
-

Table: courses

+

表:courses

 +-------------+-------------------+
@@ -40,12 +40,12 @@ Each row contains the student ID, student name, and their major.
 | major       | varchar           |       
 | mandatory   | enum              |      
 +-------------+-------------------+
-course_id is the primary key for this table. 
-mandatory is an enum type of ('Yes', 'No').
-Each row contains the course ID, course name, credits, major it belongs to, and whether the course is mandatory.
+course_id 是这张表的主键。 
+mandatory 是 ('Yes', 'No') 的枚举类型。
+每一行包含课程 ID,课程名,学分,所属专业,以及该课程是否必修。
 
-

Table: enrollments

+

表:enrollments

 +-------------+----------+
@@ -57,27 +57,28 @@ Each row contains the course ID, course name, credits, major it belongs to, and
 | grade       | varchar  |
 | GPA         | decimal  | 
 +-------------+----------+
-(student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table.
-Each row contains the student ID, course ID, semester, and grade received.
+(student_id, course_id, semester) 是这张表的主键(有不同值的列的组合)。
+这张表的每一行包含学生 ID,课程 ID,学期和获得的学分。
 
-

Write a solution to find the students who meet the following criteria:

+

编写一个解决方案来查找满足下述标准的学生:

-

Return the result table ordered by student_id in ascending order.

+

返回结果表以 student_id 升序 排序。

 

-

Example:

+ +

示例:

-

Input:

+

输入:

-

students table:

+

students 表:

  +------------+------------------+------------------+
@@ -90,7 +91,7 @@ Each row contains the student ID, course ID, semester, and grade received.
  +------------+------------------+------------------+
  
-

courses table:

+

courses 表:

  +-----------+-------------------+---------+------------------+----------+
@@ -107,7 +108,7 @@ Each row contains the student ID, course ID, semester, and grade received.
  +-----------+-------------------+---------+------------------+----------+
  
-

enrollments table:

+

enrollments 表:

  +------------+-----------+-------------+-------+-----+
@@ -128,7 +129,7 @@ Each row contains the student ID, course ID, semester, and grade received.
  +------------+-----------+-------------+-------+-----+
  
-

Output:

+

输出:

  +------------+
@@ -139,16 +140,16 @@ Each row contains the student ID, course ID, semester, and grade received.
  +------------+
  
-

Explanation:

+

解释:

    -
  • Alice (student_id 1) is a Computer Science major and has taken both Algorithms and Data Structures, receiving an A in both. She has also taken Machine Learning and Operating Systems as electives, receiving an A and B respectively.
  • -
  • Bob (student_id 2) is a Computer Science major but did not receive an A in all required courses.
  • -
  • Charlie (student_id 3) is a Mathematics major and has taken both Calculus and Linear Algebra, receiving an A in both. He has also taken Probability and Statistics as electives, receiving an A and B respectively.
  • -
  • David (student_id 4) is a Mathematics major but did not receive an A in all required courses.
  • +
  • Alice (student_id 1) 是计算机科学专业并且修了 Algorithms 和 Data Structures,都取得了 A。她同时选修了 Machine Learning 和 Operating Systems,分别取得了 A 和 B。
  • +
  • Bob (student_id 2) 是计算机科学专业但没有在所有需求的课程中取得 A。
  • +
  • Charlie (student_id 3) 是数学专业并且修了 Calculus 和 Linear Algebra,都取得了 A。他同时选修了 Probability 和 Statistics,分别取得了 A 和 B。
  • +
  • David (student_id 4) 是数学专业但没有在所有需要的课程中取得 A。
-

Note: Output table is ordered by student_id in ascending order.

+

注意:输出表以 student_id 升序排序。

@@ -159,11 +160,11 @@ Each row contains the student ID, course ID, semester, and grade received. ### 方法一:连接 + 分组 + 条件过滤 -我们首先计算出每个学生的平均 GPA,记录在临时表 `T` 中。 +我们首先筛选出平均 GPA 大于等于 2.5 的学生,记录在 `T` 表中。 -然后,我们将 `students` 表与 `courses` 表按照 `major` 进行连接,然后与 `T` 表按照 `student_id` 进行连接,再与 `enrollments` 表按照 `student_id` 和 `course_id` 进行左连接。 +然后,我们将 `T` 表与 `students` 表按照 `student_id` 进行连接,然后与 `courses` 表按照 `major` 进行连接,再与 `enrollments` 表按照 `student_id` 和 `course_id` 进行左连接。 -接下来,我们筛选出平均 GPA 大于等于 2.5 的学生,并按照学生 ID 进行分组,然后使用 `HAVING` 子句过滤出符合条件的学生,最后按照学生 ID 进行排序。 +接下来,我们按照学生 ID 进行分组,然后使用 `HAVING` 子句过滤出符合条件的学生,最后按照学生 ID 进行排序。 @@ -173,17 +174,17 @@ Each row contains the student ID, course ID, semester, and grade received. # Write your MySQL query statement below WITH T AS ( - SELECT student_id, AVG(GPA) AS avg_gpa + SELECT student_id FROM enrollments GROUP BY 1 + HAVING AVG(GPA) >= 2.5 ) SELECT student_id FROM - students + T + JOIN students USING (student_id) JOIN courses USING (major) - JOIN T USING (student_id) LEFT JOIN enrollments USING (student_id, course_id) -WHERE avg_gpa >= 2.5 GROUP BY 1 HAVING SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes') diff --git a/solution/3100-3199/3188.Find Top Scoring Students II/README_EN.md b/solution/3100-3199/3188.Find Top Scoring Students II/README_EN.md index 0892957add9cb..8dadc8819c3d3 100644 --- a/solution/3100-3199/3188.Find Top Scoring Students II/README_EN.md +++ b/solution/3100-3199/3188.Find Top Scoring Students II/README_EN.md @@ -159,11 +159,11 @@ Each row contains the student ID, course ID, semester, and grade received. ### Solution 1: Joining + Grouping + Conditional Filtering -First, we calculate the average GPA of each student and store it in a temporary table `T`. +First, we filter out students with an average GPA greater than or equal to 2.5 and record them in table `T`. -Next, we join the `students` table with the `courses` table based on `major`, and then join with the `T` table based on `student_id`, followed by a left join with the `enrollments` table based on `student_id` and `course_id`. +Next, we join the `T` table with the `students` table based on `student_id`, then join with the `courses` table based on `major`, and finally perform a left join with the `enrollments` table based on `student_id` and `course_id`. -After that, we filter out students with an average GPA greater than or equal to 2.5, group by student ID, use the `HAVING` clause to filter students who meet the criteria, and finally sort by student ID. +After that, we group by student ID, use the `HAVING` clause to filter out students who meet the conditions, and finally sort by student ID. @@ -173,17 +173,17 @@ After that, we filter out students with an average GPA greater than or equal to # Write your MySQL query statement below WITH T AS ( - SELECT student_id, AVG(GPA) AS avg_gpa + SELECT student_id FROM enrollments GROUP BY 1 + HAVING AVG(GPA) >= 2.5 ) SELECT student_id FROM - students + T + JOIN students USING (student_id) JOIN courses USING (major) - JOIN T USING (student_id) LEFT JOIN enrollments USING (student_id, course_id) -WHERE avg_gpa >= 2.5 GROUP BY 1 HAVING SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes') diff --git a/solution/3100-3199/3188.Find Top Scoring Students II/Solution.sql b/solution/3100-3199/3188.Find Top Scoring Students II/Solution.sql index b47363462e016..68aa6685cd7a5 100644 --- a/solution/3100-3199/3188.Find Top Scoring Students II/Solution.sql +++ b/solution/3100-3199/3188.Find Top Scoring Students II/Solution.sql @@ -1,17 +1,17 @@ # Write your MySQL query statement below WITH T AS ( - SELECT student_id, AVG(GPA) AS avg_gpa + SELECT student_id FROM enrollments GROUP BY 1 + HAVING AVG(GPA) >= 2.5 ) SELECT student_id FROM - students + T + JOIN students USING (student_id) JOIN courses USING (major) - JOIN T USING (student_id) LEFT JOIN enrollments USING (student_id, course_id) -WHERE avg_gpa >= 2.5 GROUP BY 1 HAVING SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes') diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 6eba9483c3172..326611a1f1915 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -283,7 +283,7 @@ | 3166 | [计算停车费与时长](/solution/3100-3199/3166.Calculate%20Parking%20Fees%20and%20Duration/README.md) | `数据库` | 中等 | 🔒 | | 3172 | [第二天验证](/solution/3100-3199/3172.Second%20Day%20Verification/README.md) | `数据库` | 简单 | 🔒 | | 3182 | [查找得分最高的学生](/solution/3100-3199/3182.Find%20Top%20Scoring%20Students/README.md) | `数据库` | 中等 | 🔒 | -| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 | +| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 | ## 版权 diff --git a/solution/README.md b/solution/README.md index ed261dca9be6e..f6ecd0e1625dd 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3198,7 +3198,7 @@ | 3185 | [构成整天的下标对数目 II](/solution/3100-3199/3185.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20II/README.md) | `数组`,`哈希表`,`计数` | 中等 | 第 402 场周赛 | | 3186 | [施咒的最大总伤害](/solution/3100-3199/3186.Maximum%20Total%20Damage%20With%20Spell%20Casting/README.md) | `数组`,`哈希表`,`双指针`,`二分查找`,`动态规划`,`计数`,`排序` | 中等 | 第 402 场周赛 | | 3187 | [数组中的峰值](/solution/3100-3199/3187.Peaks%20in%20Array/README.md) | `树状数组`,`线段树`,`数组` | 困难 | 第 402 场周赛 | -| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 | +| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 | ## 版权