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 sql solution to lc problem: No.3156 #2859

Merged
merged 2 commits into from
May 20, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md
---

<!-- problem:start -->

# [3156. 员工任务持续时间和并发任务 🔒](https://leetcode.cn/problems/employee-task-duration-and-concurrent-tasks)

[English Version](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md)

## 题目描述

<!-- description:start -->

<p>表:<code>Tasks</code></p>

<pre>
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| task_id | int |
| employee_id | int |
| start_time | datetime |
| end_time | datetime |
+---------------+----------+
(task_id, employee_id) 是这张表的主键。
这张表的每一行包含任务标识,员工标识和每个任务的开始和结束时间。
</pre>

<p>编写一个解决方案来查找 <strong>每个</strong> 员工的任务 <strong>总持续时间</strong> 以及员工在任何时间点处理的 <strong>最大并发任务数</strong>。总时长应该 <strong>舍入</strong> 到最近的 <strong>整小时</strong>。</p>

<p>返回结果表以&nbsp;<code>employee_id</code><strong> <em>升序</em></strong><em>&nbsp;排序。</em></p>

<p>结果格式如下所示。</p>

<p>&nbsp;</p>

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

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

<p>Tasks 表:</p>

<pre class="example-io">
+---------+-------------+---------------------+---------------------+
| task_id | employee_id | start_time | end_time |
+---------+-------------+---------------------+---------------------+
| 1 | 1001 | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
| 2 | 1001 | 2023-05-01 08:30:00 | 2023-05-01 10:30:00 |
| 3 | 1001 | 2023-05-01 11:00:00 | 2023-05-01 12:00:00 |
| 7 | 1001 | 2023-05-01 13:00:00 | 2023-05-01 15:30:00 |
| 4 | 1002 | 2023-05-01 09:00:00 | 2023-05-01 10:00:00 |
| 5 | 1002 | 2023-05-01 09:30:00 | 2023-05-01 11:30:00 |
| 6 | 1003 | 2023-05-01 14:00:00 | 2023-05-01 16:00:00 |
+---------+-------------+---------------------+---------------------+
</pre>

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

<pre class="example-io">
+-------------+------------------+----------------------+
| employee_id | total_task_hours | max_concurrent_tasks |
+-------------+------------------+----------------------+
| 1001 | 6 | 2 |
| 1002 | 2 | 2 |
| 1003 | 2 | 1 |
+-------------+------------------+----------------------+
</pre>

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

<ul>
<li>对于员工 ID 1001:
<ul>
<li>任务 1 和任务 2 从 08:30 到&nbsp;09:00 重叠(30 分钟)。</li>
<li>任务 7 持续时间为 150 分钟(2 小时 30 分钟)。</li>
<li>总工作小时:60(任务 1)+ 120(任务 2)+ 60(任务&nbsp;3)+ 150(任务 7)- 30(重叠)= 360 分钟 = 6 小时。</li>
<li>最大并发任务:2 (重叠期间)。</li>
</ul>
</li>
<li>对于员工 ID 1002:
<ul>
<li>任务 4 和任务 5 从 09:30 到&nbsp;10:00 重叠(30 分钟)。</li>
<li>总工作时间:60 (任务&nbsp;4)+ 120(任务 5)- 30(重叠)= 150 分钟 = 2 小时 30 分钟。</li>
<li>总工作小时:(舍入后):2 小时。</li>
<li>最大并发任务:2 (重叠期间)。</li>
</ul>
</li>
<li>对于员工 ID 1003:
<ul>
<li>没有重叠的工作。</li>
<li>总工作时间:120 分钟 = 2 小时。</li>
<li>最大并发任务:1。</li>
</ul>
</li>
</ul>

<p><b>注意:</b>输出表以 employee_id 升序排序。</p>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT employee_id, start_time AS st
FROM Tasks
UNION DISTINCT
SELECT DISTINCT employee_id, end_time AS st
FROM Tasks
),
P AS (
SELECT
*,
LEAD(st) OVER (
PARTITION BY employee_id
ORDER BY st
) AS ed
FROM T
),
S AS (
SELECT
P.*,
COUNT(1) AS concurrent_count
FROM
P
INNER JOIN Tasks USING (employee_id)
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
GROUP BY 1, 2, 3
)
SELECT
employee_id,
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
MAX(concurrent_count) AS max_concurrent_tasks
FROM S
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md
---

<!-- problem:start -->

# [3156. Employee Task Duration and Concurrent Tasks 🔒](https://leetcode.com/problems/employee-task-duration-and-concurrent-tasks)

[中文文档](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md)

## Description

<!-- description:start -->

<p>Table: <code>Tasks</code></p>

<pre>
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| task_id | int |
| employee_id | int |
| start_time | datetime |
| end_time | datetime |
+---------------+----------+
(task_id, employee_id) is the primary key for this table.
Each row in this table contains the task identifier, the employee identifier, and the start and end times of each task.
</pre>

<p>Write a solution to find the <strong>total duration</strong> of tasks for <strong>each</strong> employee and the <strong>maximum number of concurrent tasks</strong> an employee handled at <strong>any point in time</strong>. The total duration should be <strong>rounded down</strong> to the nearest number of <strong>full hours</strong>.</p>

<p>Return <em>the result table ordered by</em>&nbsp;<code>employee_id</code><strong> <em>ascending</em></strong><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>Tasks table:</p>

<pre class="example-io">
+---------+-------------+---------------------+---------------------+
| task_id | employee_id | start_time | end_time |
+---------+-------------+---------------------+---------------------+
| 1 | 1001 | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
| 2 | 1001 | 2023-05-01 08:30:00 | 2023-05-01 10:30:00 |
| 3 | 1001 | 2023-05-01 11:00:00 | 2023-05-01 12:00:00 |
| 7 | 1001 | 2023-05-01 13:00:00 | 2023-05-01 15:30:00 |
| 4 | 1002 | 2023-05-01 09:00:00 | 2023-05-01 10:00:00 |
| 5 | 1002 | 2023-05-01 09:30:00 | 2023-05-01 11:30:00 |
| 6 | 1003 | 2023-05-01 14:00:00 | 2023-05-01 16:00:00 |
+---------+-------------+---------------------+---------------------+
</pre>

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

<pre class="example-io">
+-------------+------------------+----------------------+
| employee_id | total_task_hours | max_concurrent_tasks |
+-------------+------------------+----------------------+
| 1001 | 6 | 2 |
| 1002 | 2 | 2 |
| 1003 | 2 | 1 |
+-------------+------------------+----------------------+
</pre>

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

<ul>
<li>For employee ID 1001:
<ul>
<li>Task 1 and Task 2 overlap from 08:30 to 09:00 (30 minutes).</li>
<li>Task 7 has a duration of 150 minutes (2 hours and 30 minutes).</li>
<li>Total task time: 60 (Task 1) + 120 (Task 2) + 60 (Task 3) + 150 (Task 7) - 30 (overlap) = 360 minutes = 6 hours.</li>
<li>Maximum concurrent tasks: 2 (during the overlap period).</li>
</ul>
</li>
<li>For employee ID 1002:
<ul>
<li>Task 4 and Task 5 overlap from 09:30 to 10:00 (30 minutes).</li>
<li>Total task time: 60 (Task 4) + 120 (Task 5) - 30 (overlap) = 150 minutes = 2 hours and 30 minutes.</li>
<li>Total task hours (rounded down): 2 hours.</li>
<li>Maximum concurrent tasks: 2 (during the overlap period).</li>
</ul>
</li>
<li>For employee ID 1003:
<ul>
<li>No overlapping tasks.</li>
<li>Total task time: 120 minutes = 2 hours.</li>
<li>Maximum concurrent tasks: 1.</li>
</ul>
</li>
</ul>

<p><b>Note:</b> Output table is ordered by employee_id in ascending order.</p>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### MySQL

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT employee_id, start_time AS st
FROM Tasks
UNION DISTINCT
SELECT DISTINCT employee_id, end_time AS st
FROM Tasks
),
P AS (
SELECT
*,
LEAD(st) OVER (
PARTITION BY employee_id
ORDER BY st
) AS ed
FROM T
),
S AS (
SELECT
P.*,
COUNT(1) AS concurrent_count
FROM
P
INNER JOIN Tasks USING (employee_id)
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
GROUP BY 1, 2, 3
)
SELECT
employee_id,
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
MAX(concurrent_count) AS max_concurrent_tasks
FROM S
GROUP BY 1
ORDER BY 1;
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Write your MySQL query statement below
WITH
T AS (
SELECT DISTINCT employee_id, start_time AS st
FROM Tasks
UNION DISTINCT
SELECT DISTINCT employee_id, end_time AS st
FROM Tasks
),
P AS (
SELECT
*,
LEAD(st) OVER (
PARTITION BY employee_id
ORDER BY st
) AS ed
FROM T
),
S AS (
SELECT
P.*,
COUNT(1) AS concurrent_count
FROM
P
INNER JOIN Tasks USING (employee_id)
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
GROUP BY 1, 2, 3
)
SELECT
employee_id,
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
MAX(concurrent_count) AS max_concurrent_tasks
FROM S
GROUP BY 1
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 @@ -279,6 +279,7 @@
| 3126 | [服务器利用时间](/solution/3100-3199/3126.Server%20Utilization%20Time/README.md) | `数据库` | 中等 | 🔒 |
| 3140 | [连续空余座位 II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | `数据库` | 中等 | 🔒 |
| 3150 | [无效的推文 II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | `数据库` | 简单 | 🔒 |
| 3156 | [员工任务持续时间和并发任务](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/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 @@ -277,6 +277,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README_EN.md) | `Database` | Medium | 🔒 |
| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | `Database` | Medium | 🔒 |
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README_EN.md) | `Database` | Easy | 🔒 |
| 3156 | [Employee Task Duration and Concurrent Tasks](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md) | | Hard | 🔒 |

## Copyright

Expand Down
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3166,6 +3166,7 @@
| 3153 | [所有数对中数位不同之和](/solution/3100-3199/3153.Sum%20of%20Digit%20Differences%20of%20All%20Pairs/README.md) | | 中等 | 第 398 场周赛 |
| 3154 | [到达第 K 级台阶的方案数](/solution/3100-3199/3154.Find%20Number%20of%20Ways%20to%20Reach%20the%20K-th%20Stair/README.md) | | 困难 | 第 398 场周赛 |
| 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md) | | 中等 | 🔒 |
| 3156 | [员工任务持续时间和并发任务](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/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 @@ -3164,6 +3164,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3153 | [Sum of Digit Differences of All Pairs](/solution/3100-3199/3153.Sum%20of%20Digit%20Differences%20of%20All%20Pairs/README_EN.md) | | Medium | Weekly Contest 398 |
| 3154 | [Find Number of Ways to Reach the K-th Stair](/solution/3100-3199/3154.Find%20Number%20of%20Ways%20to%20Reach%20the%20K-th%20Stair/README_EN.md) | | Hard | Weekly Contest 398 |
| 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md) | | Medium | 🔒 |
| 3156 | [Employee Task Duration and Concurrent Tasks](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md) | | Hard | 🔒 |

## Copyright

Expand Down
Loading