Skip to content
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

feat: add solutions to lc problems: No.2853,2854 #1634

Merged
merged 2 commits into from
Sep 16, 2023
Merged
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
84 changes: 84 additions & 0 deletions solution/2800-2899/2853.Highest Salaries Difference/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# [2853. Highest Salaries Difference](https://leetcode.cn/problems/highest-salaries-difference)

[English Version](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>Table: <code><font face="monospace">Salaries</font></code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| emp_name | varchar |
| department | varchar |
| salary | int |
+-------------+---------+
(emp_name, department) is the primary key for this table.
Each row of this table contains emp_name, department and salary. There will be <strong>at least one</strong> entry for the engineering and marketing departments.
</pre>

<p>Write an SQL query to calculate the difference between the <strong>highest</strong> salaries in the <strong>marketing</strong> and <strong>engineering</strong> <code>department</code>. Output the absolute difference in salaries.</p>

<p>Return<em> the result table.</em></p>

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

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

<pre>
<strong>Input:</strong>
Salaries table:
+----------+-------------+--------+
| emp_name | department | salary |
+----------+-------------+--------+
| Kathy | Engineering | 50000 |
| Roy | Marketing | 30000 |
| Charles | Engineering | 45000 |
| Jack | Engineering | 85000 |
| Benjamin | Marketing | 34000 |
| Anthony | Marketing | 42000 |
| Edward | Engineering | 102000 |
| Terry | Engineering | 44000 |
| Evelyn | Marketing | 53000 |
| Arthur | Engineering | 32000 |
+----------+-------------+--------+
<strong>Output:</strong>
+-------------------+
| salary_difference |
+-------------------+
| 49000 |
+-------------------+
<strong>Explanation:</strong>
- The Engineering and Marketing departments have the highest salaries of 102,000 and 53,000, respectively. Resulting in an absolute difference of 49,000.
</pre>

## 解法

<!-- 这里可写通用的实现逻辑 -->

**方法一:GROUP BY 分组**

我们可以先分别计算出每个部门的最高工资,然后再计算两个最高工资的差值。

<!-- tabs:start -->

### **SQL**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql
# Write your MySQL query statement below
SELECT max(s) - min(s) AS salary_difference
FROM
(
SELECT max(salary) AS s
FROM Salaries
GROUP BY department
) AS t;
```

<!-- tabs:end -->
74 changes: 74 additions & 0 deletions solution/2800-2899/2853.Highest Salaries Difference/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# [2853. Highest Salaries Difference](https://leetcode.com/problems/highest-salaries-difference)

[中文文档](/solution/2800-2899/2853.Highest%20Salaries%20Difference/README.md)

## Description

<p>Table: <code><font face="monospace">Salaries</font></code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| emp_name | varchar |
| department | varchar |
| salary | int |
+-------------+---------+
(emp_name, department) is the primary key for this table.
Each row of this table contains emp_name, department and salary. There will be <strong>at least one</strong> entry for the engineering and marketing departments.
</pre>

<p>Write an SQL query to calculate the difference between the <strong>highest</strong> salaries in the <strong>marketing</strong> and <strong>engineering</strong> <code>department</code>. Output the absolute difference in salaries.</p>

<p>Return<em> the result table.</em></p>

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

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

<pre>
<strong>Input:</strong>
Salaries table:
+----------+-------------+--------+
| emp_name | department | salary |
+----------+-------------+--------+
| Kathy | Engineering | 50000 |
| Roy | Marketing | 30000 |
| Charles | Engineering | 45000 |
| Jack | Engineering | 85000 |
| Benjamin | Marketing | 34000 |
| Anthony | Marketing | 42000 |
| Edward | Engineering | 102000 |
| Terry | Engineering | 44000 |
| Evelyn | Marketing | 53000 |
| Arthur | Engineering | 32000 |
+----------+-------------+--------+
<strong>Output:</strong>
+-------------------+
| salary_difference |
+-------------------+
| 49000 |
+-------------------+
<strong>Explanation:</strong>
- The Engineering and Marketing departments have the highest salaries of 102,000 and 53,000, respectively. Resulting in an absolute difference of 49,000.
</pre>

## Solutions

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
SELECT max(s) - min(s) AS salary_difference
FROM
(
SELECT max(salary) AS s
FROM Salaries
GROUP BY department
) AS t;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Write your MySQL query statement below
SELECT max(s) - min(s) AS salary_difference
FROM
(
SELECT max(salary) AS s
FROM Salaries
GROUP BY department
) AS t;
129 changes: 129 additions & 0 deletions solution/2800-2899/2854.Rolling Average Steps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# [2854. Rolling Average Steps](https://leetcode.cn/problems/rolling-average-steps)

[English Version](/solution/2800-2899/2854.Rolling%20Average%20Steps/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>Table: <code><font face="monospace">Steps</font></code></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| steps_count | int |
| steps_date | date |
+-------------+------+
(user_id, steps_date) is the primary key for this table.
Each row of this table contains user_id, steps_count, and steps_date.
</pre>

<p>Write a solution to calculate <code>3-day</code> <strong>rolling averages</strong> of steps for each user.</p>

<p>We calculate the <code>n-day</code> <strong>rolling average</strong> this way:</p>

<ul>
<li>For each day, we calculate the average of <code>n</code> consecutive days of step counts ending on that day if available, otherwise, <code>n-day</code> rolling average is not defined for it.</li>
</ul>

<p>Output the <code>user_id</code>, <code>steps_date</code>, and rolling average. Round the rolling average to <strong>two decimal places</strong>.</p>

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

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

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

<pre>
<strong>Input:</strong>
Steps table:
+---------+-------------+------------+
| user_id | steps_count | steps_date |
+---------+-------------+------------+
| 1 | 687 | 2021-09-02 |
| 1 | 395 | 2021-09-04 |
| 1 | 499 | 2021-09-05 |
| 1 | 712 | 2021-09-06 |
| 1 | 576 | 2021-09-07 |
| 2 | 153 | 2021-09-06 |
| 2 | 171 | 2021-09-07 |
| 2 | 530 | 2021-09-08 |
| 3 | 945 | 2021-09-04 |
| 3 | 120 | 2021-09-07 |
| 3 | 557 | 2021-09-08 |
| 3 | 840 | 2021-09-09 |
| 3 | 627 | 2021-09-10 |
| 5 | 382 | 2021-09-05 |
| 6 | 480 | 2021-09-01 |
| 6 | 191 | 2021-09-02 |
| 6 | 303 | 2021-09-05 |
+---------+-------------+------------+
<strong>Output:</strong>
+---------+------------+-----------------+
| user_id | steps_date | rolling_average |
+---------+------------+-----------------+
| 1 | 2021-09-06 | 535.33 |
| 1 | 2021-09-07 | 595.67 |
| 2 | 2021-09-08 | 284.67 |
| 3 | 2021-09-09 | 505.67 |
| 3 | 2021-09-10 | 674.67 |
+---------+------------+-----------------+
<strong>Explanation:</strong>
- For user id 1, the step counts for the three consecutive days up to 2021-09-06 are available. Consequently, the rolling average for this particular date is computed as (395 + 499 + 712) / 3 = 535.33.
- For user id 1, the step counts for the three consecutive days up to 2021-09-07 are available. Consequently, the rolling average for this particular date is computed as (499 + 712 + 576) / 3 = 595.67.
- For user id 2, the step counts for the three consecutive days up to 2021-09-08 are available. Consequently, the rolling average for this particular date is computed as (153 + 171 + 530) / 3 = 284.67.
- For user id 3, the step counts for the three consecutive days up to 2021-09-09 are available. Consequently, the rolling average for this particular date is computed as (120 + 557 + 840) / 3 = 505.67.
- For user id 3, the step counts for the three consecutive days up to 2021-09-10 are available. Consequently, the rolling average for this particular date is computed as (557 + 840 + 627) / 3 = 674.67.
- For user id 4 and 5, the calculation of the rolling average is not viable as there is insufficient data for the consecutive three days. Output table ordered by user_id and steps_date in ascending order.</pre>

## 解法

<!-- 这里可写通用的实现逻辑 -->

**方法一:窗口函数**

我们用窗口函数 `LAG() OVER()` 来计算每个用户当前日期与上上个日期之间的天数差,如果为 $2$,说明这两个日期之间有连续 $3$ 天的数据,我们可以利用窗口函数 `AVG() OVER()` 来计算这 $3$ 个数据的平均值。

<!-- tabs:start -->

### **SQL**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
user_id,
steps_date,
round(
avg(steps_count) OVER (
PARTITION BY user_id
ORDER BY steps_date
ROWS 2 PRECEDING
),
2
) AS rolling_average,
datediff(
steps_date,
lag(steps_date, 2) OVER (
PARTITION BY user_id
ORDER BY steps_date
)
) = 2 AS st
FROM Steps
)
SELECT
user_id,
steps_date,
rolling_average
FROM T
WHERE st = 1
ORDER BY 1, 2;
```

<!-- tabs:end -->
Loading