Skip to content

Commit ff48d0d

Browse files
committed
feat: update solutions to lc problem
1 parent 032e23b commit ff48d0d

File tree

5 files changed

+49
-48
lines changed

5 files changed

+49
-48
lines changed

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)