Skip to content

feat: add solutions to lc problem: No.0829 #3126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions solution/0800-0899/0829.Consecutive Numbers Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
35 changes: 33 additions & 2 deletions solution/0800-0899/0829.Consecutive Numbers Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,23 @@ tags:

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions solution/0800-0899/0829.Consecutive Numbers Sum/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
71 changes: 36 additions & 35 deletions solution/3100-3199/3188.Find Top Scoring Students II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Fi

<!-- problem:start -->

# [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)

## 题目描述

<!-- description:start -->

<p>Table: <code>students</code></p>
<p>表:<code>students</code></p>

<pre>
+-------------+----------+
Expand All @@ -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,学生姓名和他们的专业。
</pre>

<p>Table: <code>courses</code></p>
<p>表:<code>courses</code></p>

<pre>
+-------------+-------------------+
Expand All @@ -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 (&#39;Yes&#39;, &#39;No&#39;).
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,课程名,学分,所属专业,以及该课程是否必修。
</pre>

<p>Table: <code>enrollments</code></p>
<p>表:<code>enrollments</code></p>

<pre>
+-------------+----------+
Expand All @@ -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,学期和获得的学分。
</pre>

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

<ul>
<li>Have<strong> taken all mandatory courses</strong> and <strong>at least two</strong> elective courses offered in <strong>their major.</strong></li>
<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>
<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>
<li>已经 <strong>修完他们专业中所有的必修课程</strong> <strong>至少两个&nbsp;</strong>选修课程。</li>
<li><strong>所有必修课程</strong> 中取得等级 <strong>A</strong> 并且 <strong>选修课程</strong> 至少取得 <strong>B</strong></li>
<li>保持他们所有课程(包括不属于他们专业的)的平均&nbsp;<code>GPA</code>&nbsp;至少在&nbsp;<code>2.5</code>&nbsp;以上。</li>
</ul>

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

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<p><strong class="example">示例:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>
<p><strong>输入:</strong></p>

<p>students table:</p>
<p>students 表:</p>

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

<p>courses table:</p>
<p>courses 表:</p>

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

<p>enrollments table:</p>
<p>enrollments 表:</p>

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

<p><strong>Output:</strong></p>
<p><strong>输出:</strong></p>

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

<p><strong>Explanation:</strong></p>
<p><strong>解释:</strong></p>

<ul>
<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>
<li>Bob (student_id 2) is a Computer Science major but did not receive an A&nbsp;in all required courses.</li>
<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>
<li>David (student_id 4) is a Mathematics major but did not receive an A&nbsp;in all required courses.</li>
<li>Alice (student_id 1) 是计算机科学专业并且修了&nbsp;Algorithms&nbsp; Data Structures,都取得了 A。她同时选修了&nbsp;Machine Learning&nbsp; Operating Systems,分别取得了 A 和 B。</li>
<li>Bob (student_id 2) 是计算机科学专业但没有在所有需求的课程中取得 A。</li>
<li>Charlie (student_id 3) 是数学专业并且修了 Calculus&nbsp; Linear Algebra,都取得了 A。他同时选修了&nbsp;Probability&nbsp; Statistics,分别取得了 A 和 B。</li>
<li>David (student_id 4) 是数学专业但没有在所有需要的课程中取得 A。</li>
</ul>

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

<!-- description:end -->
Expand All @@ -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 进行排序。

<!-- tabs:start -->

Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 1 addition & 1 deletion solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | 困难 | 🔒 |

## 版权

Expand Down
2 changes: 1 addition & 1 deletion solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | 困难 | 🔒 |

## 版权

Expand Down