Skip to content

Commit ec922e1

Browse files
authored
feat: update solutions to lc problems: No.0829,3188 (#3126)
* No.0829.Consecutive Numbers Sum * No.3188.Find Top Scoring Students II
1 parent d7ce7a4 commit ec922e1

File tree

9 files changed

+119
-56
lines changed

9 files changed

+119
-56
lines changed

solution/0800-0899/0829.Consecutive Numbers Sum/README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,21 @@ tags:
5858

5959
### 方法一:数学推导
6060

61-
连续正整数构成一个等差数列($d=1$)。我们假设等差数列的第一项为 $a$,项数为 $k$, $n=(a+a+k-1)*k/2$,即 $n*2=(a*2+k-1)*k$ ①
61+
连续正整数构成一个公差 $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$ 一定是偶数
6262

63-
由于是连续正整数, $a>=1$,所以 ① 可以化为 $n*2>=(k+1)*k$,即 $k*(k+1)<=n*2$ ②
63+
由于 $a \geq 1$,所以 $n \times 2 = (a \times 2 + k - 1) \times k \geq k \times (k + 1)$
6464

65-
因此,$k$ 的范围需要满足 $k>=1$ 并且 $k*(k+1)<=n*2$。另外,我们从 ① 式可以发现,$k$ 必须能整除 $n*2$。
65+
综上,我们可以得出:
6666

67-
综上,我们枚举 $k$,累加满足条件的 $k$ 的个数即可。
67+
1. $k$ 一定能整除 $n \times 2$;
68+
2. $k \times (k + 1) \leq n \times 2$;
69+
3. $(n \times 2) / k - k + 1$ 一定是偶数。
6870

69-
时间复杂度 $O(\sqrt{n})$。
71+
我们从 $k = 1$ 开始枚举,当 $k \times (k + 1) > n \times 2$ 时,我们可以结束枚举。在枚举的过程中,我们判断 $k$ 是否能整除 $n \times 2$,并且 $(n \times 2) / k - k + 1$ 是否是偶数,如果是则满足条件,答案加一。
72+
73+
枚举结束后,返回答案即可。
74+
75+
时间复杂度 $O(\sqrt{n})$,其中 $n$ 为给定的正整数。空间复杂度 $O(1)$。
7076

7177
<!-- tabs:start -->
7278

@@ -135,6 +141,21 @@ func consecutiveNumbersSum(n int) int {
135141
}
136142
```
137143

144+
#### TypeScript
145+
146+
```ts
147+
function consecutiveNumbersSum(n: number): number {
148+
let ans = 0;
149+
n <<= 1;
150+
for (let k = 1; k * (k + 1) <= n; ++k) {
151+
if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) {
152+
++ans;
153+
}
154+
}
155+
return ans;
156+
}
157+
```
158+
138159
<!-- tabs:end -->
139160

140161
<!-- solution:end -->

solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Mathematical Derivation
61+
62+
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.
63+
64+
Given that $a \geq 1$, it follows that $n \times 2 = (a \times 2 + k - 1) \times k \geq k \times (k + 1)$.
65+
66+
In summary, we can conclude:
67+
68+
1. $k$ must divide $n \times 2$ evenly;
69+
2. $k \times (k + 1) \leq n \times 2$;
70+
3. $(n \times 2) / k - k + 1$ must be an even number.
71+
72+
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.
73+
74+
After finishing the enumeration, we return the answer.
75+
76+
The time complexity is $O(\sqrt{n})$, where $n$ is the given positive integer. The space complexity is $O(1)$.
6177

6278
<!-- tabs:start -->
6379

@@ -69,7 +85,7 @@ class Solution:
6985
n <<= 1
7086
ans, k = 0, 1
7187
while k * (k + 1) <= n:
72-
if n % k == 0 and (n // k + 1 - k) % 2 == 0:
88+
if n % k == 0 and (n // k - k + 1) % 2 == 0:
7389
ans += 1
7490
k += 1
7591
return ans
@@ -126,6 +142,21 @@ func consecutiveNumbersSum(n int) int {
126142
}
127143
```
128144

145+
#### TypeScript
146+
147+
```ts
148+
function consecutiveNumbersSum(n: number): number {
149+
let ans = 0;
150+
n <<= 1;
151+
for (let k = 1; k * (k + 1) <= n; ++k) {
152+
if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) {
153+
++ans;
154+
}
155+
}
156+
return ans;
157+
}
158+
```
159+
129160
<!-- tabs:end -->
130161

131162
<!-- solution:end -->

solution/0800-0899/0829.Consecutive Numbers Sum/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def consecutiveNumbersSum(self, n: int) -> int:
33
n <<= 1
44
ans, k = 0, 1
55
while k * (k + 1) <= n:
6-
if n % k == 0 and (n // k + 1 - k) % 2 == 0:
6+
if n % k == 0 and (n // k - k + 1) % 2 == 0:
77
ans += 1
88
k += 1
99
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function consecutiveNumbersSum(n: number): number {
2+
let ans = 0;
3+
n <<= 1;
4+
for (let k = 1; k * (k + 1) <= n; ++k) {
5+
if (n % k === 0 && (Math.floor(n / k) + 1 - k) % 2 === 0) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}

solution/3100-3199/3188.Find Top Scoring Students II/README.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Fi
66

77
<!-- problem:start -->
88

9-
# [3188. Find Top Scoring Students II 🔒](https://leetcode.cn/problems/find-top-scoring-students-ii)
9+
# [3188. 查找得分最高的学生 II 🔒](https://leetcode.cn/problems/find-top-scoring-students-ii)
1010

1111
[English Version](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md)
1212

1313
## 题目描述
1414

1515
<!-- description:start -->
1616

17-
<p>Table: <code>students</code></p>
17+
<p>表:<code>students</code></p>
1818

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

31-
<p>Table: <code>courses</code></p>
31+
<p>表:<code>courses</code></p>
3232

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

48-
<p>Table: <code>enrollments</code></p>
48+
<p>表:<code>enrollments</code></p>
4949

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

64-
<p>Write a solution to find the students who meet the following criteria:</p>
64+
<p>编写一个解决方案来查找满足下述标准的学生:</p>
6565

6666
<ul>
67-
<li>Have<strong> taken all mandatory courses</strong> and <strong>at least two</strong> elective courses offered in <strong>their major.</strong></li>
68-
<li>Achieved a grade of <strong>A</strong>&nbsp;in <strong>all mandatory courses</strong> and at least <strong>B</strong>&nbsp;in<strong> elective courses</strong>.</li>
69-
<li>Maintained an average <code>GPA</code> of at least&nbsp;<code>2.5</code> across all their courses (including those outside their major).</li>
67+
<li>已经 <strong>修完他们专业中所有的必修课程</strong> <strong>至少两个&nbsp;</strong>选修课程。</li>
68+
<li><strong>所有必修课程</strong> 中取得等级 <strong>A</strong> 并且 <strong>选修课程</strong> 至少取得 <strong>B</strong></li>
69+
<li>保持他们所有课程(包括不属于他们专业的)的平均&nbsp;<code>GPA</code>&nbsp;至少在&nbsp;<code>2.5</code>&nbsp;以上。</li>
7070
</ul>
7171

72-
<p>Return <em>the result table ordered by</em> <code>student_id</code> <em>in <strong>ascending</strong> order</em>.</p>
72+
<p>返回结果表以&nbsp;<code>student_id</code> <strong>升序&nbsp;</strong>排序。</p>
7373

7474
<p>&nbsp;</p>
75-
<p><strong class="example">Example:</strong></p>
75+
76+
<p><strong class="example">示例:</strong></p>
7677

7778
<div class="example-block">
78-
<p><strong>Input:</strong></p>
79+
<p><strong>输入:</strong></p>
7980

80-
<p>students table:</p>
81+
<p>students 表:</p>
8182

8283
<pre class="example-io">
8384
+------------+------------------+------------------+
@@ -90,7 +91,7 @@ Each row contains the student ID, course ID, semester, and grade received.
9091
+------------+------------------+------------------+
9192
</pre>
9293

93-
<p>courses table:</p>
94+
<p>courses 表:</p>
9495

9596
<pre class="example-io">
9697
+-----------+-------------------+---------+------------------+----------+
@@ -107,7 +108,7 @@ Each row contains the student ID, course ID, semester, and grade received.
107108
+-----------+-------------------+---------+------------------+----------+
108109
</pre>
109110

110-
<p>enrollments table:</p>
111+
<p>enrollments 表:</p>
111112

112113
<pre class="example-io">
113114
+------------+-----------+-------------+-------+-----+
@@ -128,7 +129,7 @@ Each row contains the student ID, course ID, semester, and grade received.
128129
+------------+-----------+-------------+-------+-----+
129130
</pre>
130131

131-
<p><strong>Output:</strong></p>
132+
<p><strong>输出:</strong></p>
132133

133134
<pre class="example-io">
134135
+------------+
@@ -139,16 +140,16 @@ Each row contains the student ID, course ID, semester, and grade received.
139140
+------------+
140141
</pre>
141142

142-
<p><strong>Explanation:</strong></p>
143+
<p><strong>解释:</strong></p>
143144

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

151-
<p><strong>Note:</strong> Output table is ordered by student_id in ascending order.</p>
152+
<p><strong>注意:</strong>输出表以 student_id 升序排序。</p>
152153
</div>
153154

154155
<!-- description:end -->
@@ -159,11 +160,11 @@ Each row contains the student ID, course ID, semester, and grade received.
159160

160161
### 方法一:连接 + 分组 + 条件过滤
161162

162-
我们首先计算出每个学生的平均 GPA,记录在临时表 `T`
163+
我们首先筛选出平均 GPA 大于等于 2.5 的学生,记录在 `T` 表中
163164

164-
然后,我们将 `students` 表与 `courses` 表按照 `major` 进行连接,然后与 `T` 表按照 `student_id` 进行连接,再与 `enrollments` 表按照 `student_id``course_id` 进行左连接。
165+
然后,我们将 `T` 表与 `students` 表按照 `student_id` 进行连接,然后与 `courses` 表按照 `major` 进行连接,再与 `enrollments` 表按照 `student_id``course_id` 进行左连接。
165166

166-
接下来,我们筛选出平均 GPA 大于等于 2.5 的学生,并按照学生 ID 进行分组,然后使用 `HAVING` 子句过滤出符合条件的学生,最后按照学生 ID 进行排序。
167+
接下来,我们按照学生 ID 进行分组,然后使用 `HAVING` 子句过滤出符合条件的学生,最后按照学生 ID 进行排序。
167168

168169
<!-- tabs:start -->
169170

@@ -173,17 +174,17 @@ Each row contains the student ID, course ID, semester, and grade received.
173174
# Write your MySQL query statement below
174175
WITH
175176
T AS (
176-
SELECT student_id, AVG(GPA) AS avg_gpa
177+
SELECT student_id
177178
FROM enrollments
178179
GROUP BY 1
180+
HAVING AVG(GPA) >= 2.5
179181
)
180182
SELECT student_id
181183
FROM
182-
students
184+
T
185+
JOIN students USING (student_id)
183186
JOIN courses USING (major)
184-
JOIN T USING (student_id)
185187
LEFT JOIN enrollments USING (student_id, course_id)
186-
WHERE avg_gpa >= 2.5
187188
GROUP BY 1
188189
HAVING
189190
SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes')

solution/3100-3199/3188.Find Top Scoring Students II/README_EN.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ Each row contains the student ID, course ID, semester, and grade received.
159159

160160
### Solution 1: Joining + Grouping + Conditional Filtering
161161

162-
First, we calculate the average GPA of each student and store it in a temporary table `T`.
162+
First, we filter out students with an average GPA greater than or equal to 2.5 and record them in table `T`.
163163

164-
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`.
164+
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`.
165165

166-
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.
166+
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.
167167

168168
<!-- tabs:start -->
169169

@@ -173,17 +173,17 @@ After that, we filter out students with an average GPA greater than or equal to
173173
# Write your MySQL query statement below
174174
WITH
175175
T AS (
176-
SELECT student_id, AVG(GPA) AS avg_gpa
176+
SELECT student_id
177177
FROM enrollments
178178
GROUP BY 1
179+
HAVING AVG(GPA) >= 2.5
179180
)
180181
SELECT student_id
181182
FROM
182-
students
183+
T
184+
JOIN students USING (student_id)
183185
JOIN courses USING (major)
184-
JOIN T USING (student_id)
185186
LEFT JOIN enrollments USING (student_id, course_id)
186-
WHERE avg_gpa >= 2.5
187187
GROUP BY 1
188188
HAVING
189189
SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes')

solution/3100-3199/3188.Find Top Scoring Students II/Solution.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Write your MySQL query statement below
22
WITH
33
T AS (
4-
SELECT student_id, AVG(GPA) AS avg_gpa
4+
SELECT student_id
55
FROM enrollments
66
GROUP BY 1
7+
HAVING AVG(GPA) >= 2.5
78
)
89
SELECT student_id
910
FROM
10-
students
11+
T
12+
JOIN students USING (student_id)
1113
JOIN courses USING (major)
12-
JOIN T USING (student_id)
1314
LEFT JOIN enrollments USING (student_id, course_id)
14-
WHERE avg_gpa >= 2.5
1515
GROUP BY 1
1616
HAVING
1717
SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes')

solution/DATABASE_README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283
| 3166 | [计算停车费与时长](/solution/3100-3199/3166.Calculate%20Parking%20Fees%20and%20Duration/README.md) | `数据库` | 中等 | 🔒 |
284284
| 3172 | [第二天验证](/solution/3100-3199/3172.Second%20Day%20Verification/README.md) | `数据库` | 简单 | 🔒 |
285285
| 3182 | [查找得分最高的学生](/solution/3100-3199/3182.Find%20Top%20Scoring%20Students/README.md) | `数据库` | 中等 | 🔒 |
286-
| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 |
286+
| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 |
287287

288288
## 版权
289289

solution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3198,7 +3198,7 @@
31983198
| 3185 | [构成整天的下标对数目 II](/solution/3100-3199/3185.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20II/README.md) | `数组`,`哈希表`,`计数` | 中等 | 第 402 场周赛 |
31993199
| 3186 | [施咒的最大总伤害](/solution/3100-3199/3186.Maximum%20Total%20Damage%20With%20Spell%20Casting/README.md) | `数组`,`哈希表`,`双指针`,`二分查找`,`动态规划`,`计数`,`排序` | 中等 | 第 402 场周赛 |
32003200
| 3187 | [数组中的峰值](/solution/3100-3199/3187.Peaks%20in%20Array/README.md) | `树状数组`,`线段树`,`数组` | 困难 | 第 402 场周赛 |
3201-
| 3188 | [Find Top Scoring Students II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 |
3201+
| 3188 | [查找得分最高的学生 II](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) | | 困难 | 🔒 |
32023202

32033203
## 版权
32043204

0 commit comments

Comments
 (0)