From 8cf7f370bb8be86de585a7b79f7b4d64ff3cd486 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 29 Oct 2024 08:14:19 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3338 No.3338.Second Highest Salary II --- .../3338.Second Highest Salary II/README.md | 163 ++++++++++++++++++ .../README_EN.md | 163 ++++++++++++++++++ .../3338.Second Highest Salary II/Solution.py | 9 + .../Solution.sql | 16 ++ solution/DATABASE_README.md | 1 + solution/DATABASE_README_EN.md | 1 + solution/README.md | 1 + solution/README_EN.md | 1 + 8 files changed, 355 insertions(+) create mode 100644 solution/3300-3399/3338.Second Highest Salary II/README.md create mode 100644 solution/3300-3399/3338.Second Highest Salary II/README_EN.md create mode 100644 solution/3300-3399/3338.Second Highest Salary II/Solution.py create mode 100644 solution/3300-3399/3338.Second Highest Salary II/Solution.sql diff --git a/solution/3300-3399/3338.Second Highest Salary II/README.md b/solution/3300-3399/3338.Second Highest Salary II/README.md new file mode 100644 index 0000000000000..db9a754c8c20b --- /dev/null +++ b/solution/3300-3399/3338.Second Highest Salary II/README.md @@ -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: + - 数据库 +--- + + + +# [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) + +## 题目描述 + + + +

Table: employees

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

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

+ +

Return the result table ordered by emp_id in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

employees table:

+ +
++--------+--------+-----------+
+| 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        |
++--------+--------+-----------+
+
+ +

Output:

+ +
++--------+-----------+
+| emp_id | dept      |
++--------+-----------+
+| 2      | Sales     |
+| 3      | Sales     |
+| 5      | IT        |
+| 8      | Marketing |
++--------+-----------+
+
+ +

Explanation:

+ + +
+ + + +## 解法 + + + +### 方法一:窗口函数 + +我们可以使用 `DENSE_RANK()` 窗口函数来为每个部门的员工按照工资降序排名,然后筛选出排名为 $2$ 的员工即可。 + + + +#### 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") +``` + + + + + + diff --git a/solution/3300-3399/3338.Second Highest Salary II/README_EN.md b/solution/3300-3399/3338.Second Highest Salary II/README_EN.md new file mode 100644 index 0000000000000..db163728be1e3 --- /dev/null +++ b/solution/3300-3399/3338.Second Highest Salary II/README_EN.md @@ -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 +--- + + + +# [3338. Second Highest Salary II 🔒](https://leetcode.com/problems/second-highest-salary-ii) + +[中文文档](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) + +## Description + + + +

Table: employees

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

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

+ +

Return the result table ordered by emp_id in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

employees table:

+ +
++--------+--------+-----------+
+| 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        |
++--------+--------+-----------+
+
+ +

Output:

+ +
++--------+-----------+
+| emp_id | dept      |
++--------+-----------+
+| 2      | Sales     |
+| 3      | Sales     |
+| 5      | IT        |
+| 8      | Marketing |
++--------+-----------+
+
+ +

Explanation:

+ + +
+ + + +## Solutions + + + +### 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$. + + + +#### 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") +``` + + + + + + diff --git a/solution/3300-3399/3338.Second Highest Salary II/Solution.py b/solution/3300-3399/3338.Second Highest Salary II/Solution.py new file mode 100644 index 0000000000000..3f54bb57cef64 --- /dev/null +++ b/solution/3300-3399/3338.Second Highest Salary II/Solution.py @@ -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") diff --git a/solution/3300-3399/3338.Second Highest Salary II/Solution.sql b/solution/3300-3399/3338.Second Highest Salary II/Solution.sql new file mode 100644 index 0000000000000..e563c55927d8c --- /dev/null +++ b/solution/3300-3399/3338.Second Highest Salary II/Solution.sql @@ -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; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 071176cb58a8b..fd8067293be04 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -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) | `数据库` | 中等 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 6790f561816d8..1bb542179219f 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -297,6 +297,7 @@ Press Control + F(or Command + F 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 diff --git a/solution/README.md b/solution/README.md index 6ab7ee58dc52e..14ed984e7010a 100644 --- a/solution/README.md +++ b/solution/README.md @@ -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) | `数据库` | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 0aa4c58c26143..d036caecd6df9 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3346,6 +3346,7 @@ Press Control + F(or Command + F 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