Skip to content

Commit df9bd5d

Browse files
authored
feat: add sql solution to lc problem: No.3156 (#2859)
No.3156.Employee Task Duration and Concurrent Tasks
1 parent 5f6a3af commit df9bd5d

File tree

7 files changed

+354
-0
lines changed

7 files changed

+354
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
comments: true
3+
difficulty: 困难
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3156. 员工任务持续时间和并发任务 🔒](https://leetcode.cn/problems/employee-task-duration-and-concurrent-tasks)
10+
11+
[English Version](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>表:<code>Tasks</code></p>
18+
19+
<pre>
20+
+---------------+----------+
21+
| Column Name | Type |
22+
+---------------+----------+
23+
| task_id | int |
24+
| employee_id | int |
25+
| start_time | datetime |
26+
| end_time | datetime |
27+
+---------------+----------+
28+
(task_id, employee_id) 是这张表的主键。
29+
这张表的每一行包含任务标识,员工标识和每个任务的开始和结束时间。
30+
</pre>
31+
32+
<p>编写一个解决方案来查找 <strong>每个</strong> 员工的任务 <strong>总持续时间</strong> 以及员工在任何时间点处理的 <strong>最大并发任务数</strong>。总时长应该 <strong>舍入</strong> 到最近的 <strong>整小时</strong>。</p>
33+
34+
<p>返回结果表以&nbsp;<code>employee_id</code><strong> <em>升序</em></strong><em>&nbsp;排序。</em></p>
35+
36+
<p>结果格式如下所示。</p>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong class="example">示例:</strong></p>
41+
42+
<div class="example-block">
43+
<p><strong>输入:</strong></p>
44+
45+
<p>Tasks 表:</p>
46+
47+
<pre class="example-io">
48+
+---------+-------------+---------------------+---------------------+
49+
| task_id | employee_id | start_time | end_time |
50+
+---------+-------------+---------------------+---------------------+
51+
| 1 | 1001 | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
52+
| 2 | 1001 | 2023-05-01 08:30:00 | 2023-05-01 10:30:00 |
53+
| 3 | 1001 | 2023-05-01 11:00:00 | 2023-05-01 12:00:00 |
54+
| 7 | 1001 | 2023-05-01 13:00:00 | 2023-05-01 15:30:00 |
55+
| 4 | 1002 | 2023-05-01 09:00:00 | 2023-05-01 10:00:00 |
56+
| 5 | 1002 | 2023-05-01 09:30:00 | 2023-05-01 11:30:00 |
57+
| 6 | 1003 | 2023-05-01 14:00:00 | 2023-05-01 16:00:00 |
58+
+---------+-------------+---------------------+---------------------+
59+
</pre>
60+
61+
<p><strong>输出:</strong></p>
62+
63+
<pre class="example-io">
64+
+-------------+------------------+----------------------+
65+
| employee_id | total_task_hours | max_concurrent_tasks |
66+
+-------------+------------------+----------------------+
67+
| 1001 | 6 | 2 |
68+
| 1002 | 2 | 2 |
69+
| 1003 | 2 | 1 |
70+
+-------------+------------------+----------------------+
71+
</pre>
72+
73+
<p><strong>解释:</strong></p>
74+
75+
<ul>
76+
<li>对于员工 ID 1001:
77+
<ul>
78+
<li>任务 1 和任务 2 从 08:30 到&nbsp;09:00 重叠(30 分钟)。</li>
79+
<li>任务 7 持续时间为 150 分钟(2 小时 30 分钟)。</li>
80+
<li>总工作小时:60(任务 1)+ 120(任务 2)+ 60(任务&nbsp;3)+ 150(任务 7)- 30(重叠)= 360 分钟 = 6 小时。</li>
81+
<li>最大并发任务:2 (重叠期间)。</li>
82+
</ul>
83+
</li>
84+
<li>对于员工 ID 1002:
85+
<ul>
86+
<li>任务 4 和任务 5 从 09:30 到&nbsp;10:00 重叠(30 分钟)。</li>
87+
<li>总工作时间:60 (任务&nbsp;4)+ 120(任务 5)- 30(重叠)= 150 分钟 = 2 小时 30 分钟。</li>
88+
<li>总工作小时:(舍入后):2 小时。</li>
89+
<li>最大并发任务:2 (重叠期间)。</li>
90+
</ul>
91+
</li>
92+
<li>对于员工 ID 1003:
93+
<ul>
94+
<li>没有重叠的工作。</li>
95+
<li>总工作时间:120 分钟 = 2 小时。</li>
96+
<li>最大并发任务:1。</li>
97+
</ul>
98+
</li>
99+
</ul>
100+
101+
<p><b>注意:</b>输出表以 employee_id 升序排序。</p>
102+
</div>
103+
104+
<!-- description:end -->
105+
106+
## 解法
107+
108+
<!-- solution:start -->
109+
110+
### 方法一
111+
112+
<!-- tabs:start -->
113+
114+
#### MySQL
115+
116+
```sql
117+
# Write your MySQL query statement below
118+
WITH
119+
T AS (
120+
SELECT DISTINCT employee_id, start_time AS st
121+
FROM Tasks
122+
UNION DISTINCT
123+
SELECT DISTINCT employee_id, end_time AS st
124+
FROM Tasks
125+
),
126+
P AS (
127+
SELECT
128+
*,
129+
LEAD(st) OVER (
130+
PARTITION BY employee_id
131+
ORDER BY st
132+
) AS ed
133+
FROM T
134+
),
135+
S AS (
136+
SELECT
137+
P.*,
138+
COUNT(1) AS concurrent_count
139+
FROM
140+
P
141+
INNER JOIN Tasks USING (employee_id)
142+
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
143+
GROUP BY 1, 2, 3
144+
)
145+
SELECT
146+
employee_id,
147+
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
148+
MAX(concurrent_count) AS max_concurrent_tasks
149+
FROM S
150+
GROUP BY 1
151+
ORDER BY 1;
152+
```
153+
154+
<!-- tabs:end -->
155+
156+
<!-- solution:end -->
157+
158+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
comments: true
3+
difficulty: Hard
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3156. Employee Task Duration and Concurrent Tasks 🔒](https://leetcode.com/problems/employee-task-duration-and-concurrent-tasks)
10+
11+
[中文文档](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>Table: <code>Tasks</code></p>
18+
19+
<pre>
20+
+---------------+----------+
21+
| Column Name | Type |
22+
+---------------+----------+
23+
| task_id | int |
24+
| employee_id | int |
25+
| start_time | datetime |
26+
| end_time | datetime |
27+
+---------------+----------+
28+
(task_id, employee_id) is the primary key for this table.
29+
Each row in this table contains the task identifier, the employee identifier, and the start and end times of each task.
30+
</pre>
31+
32+
<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>
33+
34+
<p>Return <em>the result table ordered by</em>&nbsp;<code>employee_id</code><strong> <em>ascending</em></strong><em> order</em>.</p>
35+
36+
<p>The result format is in the following example.</p>
37+
38+
<p>&nbsp;</p>
39+
<p><strong class="example">Example:</strong></p>
40+
41+
<div class="example-block">
42+
<p><strong>Input:</strong></p>
43+
44+
<p>Tasks table:</p>
45+
46+
<pre class="example-io">
47+
+---------+-------------+---------------------+---------------------+
48+
| task_id | employee_id | start_time | end_time |
49+
+---------+-------------+---------------------+---------------------+
50+
| 1 | 1001 | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
51+
| 2 | 1001 | 2023-05-01 08:30:00 | 2023-05-01 10:30:00 |
52+
| 3 | 1001 | 2023-05-01 11:00:00 | 2023-05-01 12:00:00 |
53+
| 7 | 1001 | 2023-05-01 13:00:00 | 2023-05-01 15:30:00 |
54+
| 4 | 1002 | 2023-05-01 09:00:00 | 2023-05-01 10:00:00 |
55+
| 5 | 1002 | 2023-05-01 09:30:00 | 2023-05-01 11:30:00 |
56+
| 6 | 1003 | 2023-05-01 14:00:00 | 2023-05-01 16:00:00 |
57+
+---------+-------------+---------------------+---------------------+
58+
</pre>
59+
60+
<p><strong>Output:</strong></p>
61+
62+
<pre class="example-io">
63+
+-------------+------------------+----------------------+
64+
| employee_id | total_task_hours | max_concurrent_tasks |
65+
+-------------+------------------+----------------------+
66+
| 1001 | 6 | 2 |
67+
| 1002 | 2 | 2 |
68+
| 1003 | 2 | 1 |
69+
+-------------+------------------+----------------------+
70+
</pre>
71+
72+
<p><strong>Explanation:</strong></p>
73+
74+
<ul>
75+
<li>For employee ID 1001:
76+
<ul>
77+
<li>Task 1 and Task 2 overlap from 08:30 to 09:00 (30 minutes).</li>
78+
<li>Task 7 has a duration of 150 minutes (2 hours and 30 minutes).</li>
79+
<li>Total task time: 60 (Task 1) + 120 (Task 2) + 60 (Task 3) + 150 (Task 7) - 30 (overlap) = 360 minutes = 6 hours.</li>
80+
<li>Maximum concurrent tasks: 2 (during the overlap period).</li>
81+
</ul>
82+
</li>
83+
<li>For employee ID 1002:
84+
<ul>
85+
<li>Task 4 and Task 5 overlap from 09:30 to 10:00 (30 minutes).</li>
86+
<li>Total task time: 60 (Task 4) + 120 (Task 5) - 30 (overlap) = 150 minutes = 2 hours and 30 minutes.</li>
87+
<li>Total task hours (rounded down): 2 hours.</li>
88+
<li>Maximum concurrent tasks: 2 (during the overlap period).</li>
89+
</ul>
90+
</li>
91+
<li>For employee ID 1003:
92+
<ul>
93+
<li>No overlapping tasks.</li>
94+
<li>Total task time: 120 minutes = 2 hours.</li>
95+
<li>Maximum concurrent tasks: 1.</li>
96+
</ul>
97+
</li>
98+
</ul>
99+
100+
<p><b>Note:</b> Output table is ordered by employee_id in ascending order.</p>
101+
</div>
102+
103+
<!-- description:end -->
104+
105+
## Solutions
106+
107+
<!-- solution:start -->
108+
109+
### Solution 1
110+
111+
<!-- tabs:start -->
112+
113+
#### MySQL
114+
115+
```sql
116+
# Write your MySQL query statement below
117+
WITH
118+
T AS (
119+
SELECT DISTINCT employee_id, start_time AS st
120+
FROM Tasks
121+
UNION DISTINCT
122+
SELECT DISTINCT employee_id, end_time AS st
123+
FROM Tasks
124+
),
125+
P AS (
126+
SELECT
127+
*,
128+
LEAD(st) OVER (
129+
PARTITION BY employee_id
130+
ORDER BY st
131+
) AS ed
132+
FROM T
133+
),
134+
S AS (
135+
SELECT
136+
P.*,
137+
COUNT(1) AS concurrent_count
138+
FROM
139+
P
140+
INNER JOIN Tasks USING (employee_id)
141+
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
142+
GROUP BY 1, 2, 3
143+
)
144+
SELECT
145+
employee_id,
146+
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
147+
MAX(concurrent_count) AS max_concurrent_tasks
148+
FROM S
149+
GROUP BY 1
150+
ORDER BY 1;
151+
```
152+
153+
<!-- tabs:end -->
154+
155+
<!-- solution:end -->
156+
157+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT DISTINCT employee_id, start_time AS st
5+
FROM Tasks
6+
UNION DISTINCT
7+
SELECT DISTINCT employee_id, end_time AS st
8+
FROM Tasks
9+
),
10+
P AS (
11+
SELECT
12+
*,
13+
LEAD(st) OVER (
14+
PARTITION BY employee_id
15+
ORDER BY st
16+
) AS ed
17+
FROM T
18+
),
19+
S AS (
20+
SELECT
21+
P.*,
22+
COUNT(1) AS concurrent_count
23+
FROM
24+
P
25+
INNER JOIN Tasks USING (employee_id)
26+
WHERE P.st >= Tasks.start_time AND P.ed <= Tasks.end_time
27+
GROUP BY 1, 2, 3
28+
)
29+
SELECT
30+
employee_id,
31+
FLOOR(SUM(TIME_TO_SEC(TIMEDIFF(ed, st)) / 3600)) AS total_task_hours,
32+
MAX(concurrent_count) AS max_concurrent_tasks
33+
FROM S
34+
GROUP BY 1
35+
ORDER BY 1;

solution/DATABASE_README.md

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
| 3126 | [服务器利用时间](/solution/3100-3199/3126.Server%20Utilization%20Time/README.md) | `数据库` | 中等 | 🔒 |
280280
| 3140 | [连续空余座位 II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | `数据库` | 中等 | 🔒 |
281281
| 3150 | [无效的推文 II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | `数据库` | 简单 | 🔒 |
282+
| 3156 | [员工任务持续时间和并发任务](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md) | | 困难 | 🔒 |
282283

283284
## 版权
284285

solution/DATABASE_README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
277277
| 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README_EN.md) | `Database` | Medium | 🔒 |
278278
| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | `Database` | Medium | 🔒 |
279279
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README_EN.md) | `Database` | Easy | 🔒 |
280+
| 3156 | [Employee Task Duration and Concurrent Tasks](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md) | | Hard | 🔒 |
280281

281282
## Copyright
282283

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3166,6 +3166,7 @@
31663166
| 3153 | [所有数对中数位不同之和](/solution/3100-3199/3153.Sum%20of%20Digit%20Differences%20of%20All%20Pairs/README.md) | | 中等 | 第 398 场周赛 |
31673167
| 3154 | [到达第 K 级台阶的方案数](/solution/3100-3199/3154.Find%20Number%20of%20Ways%20to%20Reach%20the%20K-th%20Stair/README.md) | | 困难 | 第 398 场周赛 |
31683168
| 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md) | | 中等 | 🔒 |
3169+
| 3156 | [员工任务持续时间和并发任务](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README.md) | | 困难 | 🔒 |
31693170

31703171
## 版权
31713172

solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
31643164
| 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 |
31653165
| 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 |
31663166
| 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md) | | Medium | 🔒 |
3167+
| 3156 | [Employee Task Duration and Concurrent Tasks](/solution/3100-3199/3156.Employee%20Task%20Duration%20and%20Concurrent%20Tasks/README_EN.md) | | Hard | 🔒 |
31673168

31683169
## Copyright
31693170

0 commit comments

Comments
 (0)