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; +} 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:
+编写一个解决方案来查找满足下述标准的学生:
GPA
of at least 2.5
across all their courses (including those outside their major).GPA
至少在 2.5
以上。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:
+解释:
Note: Output table is ordered by student_id in ascending order.
+注意:输出表以 student_id 升序排序。