|
| 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> <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> </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 --> |
0 commit comments