Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problem: No.3338 #3684

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 1 commit into from
Oct 29, 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
163 changes: 163 additions & 0 deletions solution/3300-3399/3338.Second Highest Salary II/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md
tags:
- 数据库
---

<!-- problem:start -->

# [3338. Second Highest Salary II 🔒](https://leetcode.cn/problems/second-highest-salary-ii)

[English Version](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md)

## 题目描述

<!-- description:start -->

<p>Table: <code>employees</code></p>

<pre>
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| emp_id | int |
| salary | int |
| dept | varchar |
+------------------+---------+
emp_id is the unique key for this table.
Each row of this table contains information about an employee including their ID, name, manager, salary, department, start date, and building assignment.
</pre>

<p>Write a solution to find the employees who earn the <strong>second-highest salary</strong> in each department. If <strong>multiple employees have the second-highest salary</strong>, <strong>include</strong> <strong>all employees</strong> with <strong>that salary</strong>.</p>

<p>Return <em>the result table</em> <em>ordered by</em> <code>emp_id</code> <em>in</em> <em><strong>ascending</strong></em> <em>order</em>.</p>

<p>The result format is in the following example.</p>

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

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

<p>employees table:</p>

<pre class="example-io">
+--------+--------+-----------+
| emp_id | salary | dept |
+--------+--------+-----------+
| 1 | 70000 | Sales |
| 2 | 80000 | Sales |
| 3 | 80000 | Sales |
| 4 | 90000 | Sales |
| 5 | 55000 | IT |
| 6 | 65000 | IT |
| 7 | 65000 | IT |
| 8 | 50000 | Marketing |
| 9 | 55000 | Marketing |
| 10 | 55000 | HR |
+--------+--------+-----------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+--------+-----------+
| emp_id | dept |
+--------+-----------+
| 2 | Sales |
| 3 | Sales |
| 5 | IT |
| 8 | Marketing |
+--------+-----------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>Sales Department</strong>:

<ul>
<li>Highest salary is 90000 (emp_id: 4)</li>
<li>Second-highest salary is 80000 (emp_id: 2, 3)</li>
<li>Both employees with salary 80000 are included</li>
</ul>
</li>
<li><strong>IT Department</strong>:
<ul>
<li>Highest salary is 65000 (emp_id: 6, 7)</li>
<li>Second-highest salary is 55000 (emp_id: 5)</li>
<li>Only emp_id 5 is included as they have the second-highest salary</li>
</ul>
</li>
<li><strong>Marketing Department</strong>:
<ul>
<li>Highest salary is 55000 (emp_id: 9)</li>
<li>Second-highest salary is 50000 (emp_id: 8)</li>
<li>Employee 8&nbsp;is included</li>
</ul>
</li>
<li><strong>HR Department</strong>:
<ul>
<li>Only has one employee</li>
<li>Not included in the result as it has fewer than 2 employees</li>
</ul>
</li>

</ul>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:窗口函数

我们可以使用 `DENSE_RANK()` 窗口函数来为每个部门的员工按照工资降序排名,然后筛选出排名为 $2$ 的员工即可。

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
emp_id,
dept,
DENSE_RANK() OVER (
PARTITION BY dept
ORDER BY salary DESC
) rk
FROM Employees
)
SELECT emp_id, dept
FROM T
WHERE rk = 2
ORDER BY 1;
```

#### Pandas

```python
import pandas as pd


def find_second_highest_salary(employees: pd.DataFrame) -> pd.DataFrame:
employees["rk"] = employees.groupby("dept")["salary"].rank(
method="dense", ascending=False
)
second_highest = employees[employees["rk"] == 2][["emp_id", "dept"]]
return second_highest.sort_values(by="emp_id")
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
163 changes: 163 additions & 0 deletions solution/3300-3399/3338.Second Highest Salary II/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md
tags:
- Database
---

<!-- problem:start -->

# [3338. Second Highest Salary II 🔒](https://leetcode.com/problems/second-highest-salary-ii)

[中文文档](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md)

## Description

<!-- description:start -->

<p>Table: <code>employees</code></p>

<pre>
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| emp_id | int |
| salary | int |
| dept | varchar |
+------------------+---------+
emp_id is the unique key for this table.
Each row of this table contains information about an employee including their ID, name, manager, salary, department, start date, and building assignment.
</pre>

<p>Write a solution to find the employees who earn the <strong>second-highest salary</strong> in each department. If <strong>multiple employees have the second-highest salary</strong>, <strong>include</strong> <strong>all employees</strong> with <strong>that salary</strong>.</p>

<p>Return <em>the result table</em> <em>ordered by</em> <code>emp_id</code> <em>in</em> <em><strong>ascending</strong></em> <em>order</em>.</p>

<p>The result format is in the following example.</p>

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

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

<p>employees table:</p>

<pre class="example-io">
+--------+--------+-----------+
| emp_id | salary | dept |
+--------+--------+-----------+
| 1 | 70000 | Sales |
| 2 | 80000 | Sales |
| 3 | 80000 | Sales |
| 4 | 90000 | Sales |
| 5 | 55000 | IT |
| 6 | 65000 | IT |
| 7 | 65000 | IT |
| 8 | 50000 | Marketing |
| 9 | 55000 | Marketing |
| 10 | 55000 | HR |
+--------+--------+-----------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+--------+-----------+
| emp_id | dept |
+--------+-----------+
| 2 | Sales |
| 3 | Sales |
| 5 | IT |
| 8 | Marketing |
+--------+-----------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li><strong>Sales Department</strong>:

<ul>
<li>Highest salary is 90000 (emp_id: 4)</li>
<li>Second-highest salary is 80000 (emp_id: 2, 3)</li>
<li>Both employees with salary 80000 are included</li>
</ul>
</li>
<li><strong>IT Department</strong>:
<ul>
<li>Highest salary is 65000 (emp_id: 6, 7)</li>
<li>Second-highest salary is 55000 (emp_id: 5)</li>
<li>Only emp_id 5 is included as they have the second-highest salary</li>
</ul>
</li>
<li><strong>Marketing Department</strong>:
<ul>
<li>Highest salary is 55000 (emp_id: 9)</li>
<li>Second-highest salary is 50000 (emp_id: 8)</li>
<li>Employee 8&nbsp;is included</li>
</ul>
</li>
<li><strong>HR Department</strong>:
<ul>
<li>Only has one employee</li>
<li>Not included in the result as it has fewer than 2 employees</li>
</ul>
</li>

</ul>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Window Function

We can use the `DENSE_RANK()` window function to rank employees in each department by salary in descending order, and then filter out the employees with a rank of $2$.

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
emp_id,
dept,
DENSE_RANK() OVER (
PARTITION BY dept
ORDER BY salary DESC
) rk
FROM Employees
)
SELECT emp_id, dept
FROM T
WHERE rk = 2
ORDER BY 1;
```

#### Pandas

```python
import pandas as pd


def find_second_highest_salary(employees: pd.DataFrame) -> pd.DataFrame:
employees["rk"] = employees.groupby("dept")["salary"].rank(
method="dense", ascending=False
)
second_highest = employees[employees["rk"] == 2][["emp_id", "dept"]]
return second_highest.sort_values(by="emp_id")
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
9 changes: 9 additions & 0 deletions solution/3300-3399/3338.Second Highest Salary II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pandas as pd


def find_second_highest_salary(employees: pd.DataFrame) -> pd.DataFrame:
employees["rk"] = employees.groupby("dept")["salary"].rank(
method="dense", ascending=False
)
second_highest = employees[employees["rk"] == 2][["emp_id", "dept"]]
return second_highest.sort_values(by="emp_id")
16 changes: 16 additions & 0 deletions solution/3300-3399/3338.Second Highest Salary II/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT
emp_id,
dept,
DENSE_RANK() OVER (
PARTITION BY dept
ORDER BY salary DESC
) rk
FROM Employees
)
SELECT emp_id, dept
FROM T
WHERE rk = 2
ORDER BY 1;
1 change: 1 addition & 0 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
| 3308 | [寻找表现最佳的司机](/solution/3300-3399/3308.Find%20Top%20Performing%20Driver/README.md) | `数据库` | 中等 | 🔒 |
| 3322 | [英超积分榜排名 III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README.md) | `数据库` | 中等 | 🔒 |
| 3328 | [查找每个州的城市 II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | `数据库` | 中等 | 🔒 |
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/DATABASE_README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3308 | [Find Top Performing Driver](/solution/3300-3399/3308.Find%20Top%20Performing%20Driver/README_EN.md) | `Database` | Medium | 🔒 |
| 3322 | [Premier League Table Ranking III](/solution/3300-3399/3322.Premier%20League%20Table%20Ranking%20III/README_EN.md) | `Database` | Medium | 🔒 |
| 3328 | [Find Cities in Each State II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README_EN.md) | `Database` | Medium | 🔒 |
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |

## Copyright

Expand Down
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3348,6 +3348,7 @@
| 3335 | [字符串转换后的长度 I](/solution/3300-3399/3335.Total%20Characters%20in%20String%20After%20Transformations%20I/README.md) | | 中等 | 第 421 场周赛 |
| 3336 | [最大公约数相等的子序列数量](/solution/3300-3399/3336.Find%20the%20Number%20of%20Subsequences%20With%20Equal%20GCD/README.md) | | 困难 | 第 421 场周赛 |
| 3337 | [字符串转换后的长度 II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README.md) | | 困难 | 第 421 场周赛 |
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3346,6 +3346,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3335 | [Total Characters in String After Transformations I](/solution/3300-3399/3335.Total%20Characters%20in%20String%20After%20Transformations%20I/README_EN.md) | | Medium | Weekly Contest 421 |
| 3336 | [Find the Number of Subsequences With Equal GCD](/solution/3300-3399/3336.Find%20the%20Number%20of%20Subsequences%20With%20Equal%20GCD/README_EN.md) | | Hard | Weekly Contest 421 |
| 3337 | [Total Characters in String After Transformations II](/solution/3300-3399/3337.Total%20Characters%20in%20String%20After%20Transformations%20II/README_EN.md) | | Hard | Weekly Contest 421 |
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |

## Copyright

Expand Down
Loading