You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md
+33-2Lines changed: 33 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,23 @@ tags:
57
57
58
58
<!-- solution:start -->
59
59
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)$.
61
77
62
78
<!-- tabs:start -->
63
79
@@ -69,7 +85,7 @@ class Solution:
69
85
n <<=1
70
86
ans, k =0, 1
71
87
while k * (k +1) <= n:
72
-
if n % k ==0and (n // k +1- k) %2==0:
88
+
if n % k ==0and (n // k - k+1) %2==0:
73
89
ans +=1
74
90
k +=1
75
91
return ans
@@ -126,6 +142,21 @@ func consecutiveNumbersSum(n int) int {
<p>Write a solution to find the students who meet the following criteria:</p>
64
+
<p>编写一个解决方案来查找满足下述标准的学生:</p>
65
65
66
66
<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> in <strong>all mandatory courses</strong> and at least <strong>B</strong> in<strong> elective courses</strong>.</li>
69
-
<li>Maintained an average <code>GPA</code> of at least <code>2.5</code> across all their courses (including those outside their major).</li>
@@ -139,16 +140,16 @@ Each row contains the student ID, course ID, semester, and grade received.
139
140
+------------+
140
141
</pre>
141
142
142
-
<p><strong>Explanation:</strong></p>
143
+
<p><strong>解释:</strong></p>
143
144
144
145
<ul>
145
-
<li>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.</li>
146
-
<li>Bob (student_id 2) is a Computer Science major but did not receive an A in all required courses.</li>
147
-
<li>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.</li>
148
-
<li>David (student_id 4) is a Mathematics major but did not receive an A in all required courses.</li>
146
+
<li>Alice (student_id 1) 是计算机科学专业并且修了 Algorithms 和 Data Structures,都取得了 A。她同时选修了 Machine Learning 和 Operating Systems,分别取得了 A 和 B。</li>
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`.
163
163
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`.
165
165
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.
167
167
168
168
<!-- tabs:start -->
169
169
@@ -173,17 +173,17 @@ After that, we filter out students with an average GPA greater than or equal to
173
173
# Write your MySQL query statement below
174
174
WITH
175
175
T AS (
176
-
SELECT student_id, AVG(GPA) AS avg_gpa
176
+
SELECT student_id
177
177
FROM enrollments
178
178
GROUP BY1
179
+
HAVINGAVG(GPA) >=2.5
179
180
)
180
181
SELECT student_id
181
182
FROM
182
-
students
183
+
T
184
+
JOIN students USING (student_id)
183
185
JOIN courses USING (major)
184
-
JOIN T USING (student_id)
185
186
LEFT JOIN enrollments USING (student_id, course_id)
0 commit comments