Employee Task Duration and Concurrent Tasks Problem
Description
LeetCode Problem 3156.
Table: Tasks
+---------------+----------+
| 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.
Write a solution to find the total duration of tasks for each employee and the maximum number of concurrent tasks an employee handled at any point in time. The total duration should be rounded down to the nearest number of full hours.
Return the result table ordered by employee_id ascending order.
The result format is in the following example.
Example:
Input:
Tasks table:
+---------+-------------+---------------------+---------------------+
| 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 |
+---------+-------------+---------------------+---------------------+
Output:
+-------------+------------------+----------------------+
| employee_id | total_task_hours | max_concurrent_tasks |
+-------------+------------------+----------------------+
| 1001 | 6 | 2 |
| 1002 | 2 | 2 |
| 1003 | 2 | 1 |
+-------------+------------------+----------------------+
Explanation:
- For employee ID 1001:
- Task 1 and Task 2 overlap from 08:30 to 09:00 (30 minutes).
- Task 7 has a duration of 150 minutes (2 hours and 30 minutes).
- Total task time: 60 (Task 1) + 120 (Task 2) + 60 (Task 3) + 150 (Task 7) - 30 (overlap) = 360 minutes = 6 hours.
- Maximum concurrent tasks: 2 (during the overlap period).
- For employee ID 1002:
- Task 4 and Task 5 overlap from 09:30 to 10:00 (30 minutes).
- Total task time: 60 (Task 4) + 120 (Task 5) - 30 (overlap) = 150 minutes = 2 hours and 30 minutes.
- Total task hours (rounded down): 2 hours.
- Maximum concurrent tasks: 2 (during the overlap period).
- For employee ID 1003:
- No overlapping tasks.
- Total task time: 120 minutes = 2 hours.
- Maximum concurrent tasks: 1.
Note: Output table is ordered by employee_id in ascending order.
Solution
Approach: Cut the timeline at every start and end, then count overlaps
Adding up task lengths double counts overlapping time, and the maximum concurrency is a property of moments, not of tasks. Both are easy once an employee's timeline is cut into small pieces that no task boundary crosses.
- For each employee collect every distinct start and end time and sort them. Pair each time with the next one (
LEAD) to get consecutive slices[st, ed]. - A task covers a slice when
start_time <= standed <= end_time. Joining slices to tasks and counting rows per slice gives the number of concurrent tasks in that slice. Slices covered by no task (gaps between tasks) disappear in the inner join. - The total duration is the sum of the lengths of the remaining slices, so overlapping time is counted once; round it down to whole hours at the end. The maximum concurrency is the largest slice count.
Floor only after summing: flooring each slice (or each task) would lose the fractional hours.
The Pandas version builds the same slices per employee and counts the covering tasks in a loop.
WITH points AS (
SELECT employee_id, start_time AS t FROM Tasks
UNION
SELECT employee_id, end_time AS t FROM Tasks
),
slices AS (
SELECT employee_id,
t AS st,
LEAD(t) OVER (PARTITION BY employee_id ORDER BY t) AS ed
FROM points
),
covered AS (
SELECT s.employee_id,
s.st,
s.ed,
COUNT(*) AS concurrent
FROM slices AS s
JOIN Tasks AS k
ON k.employee_id = s.employee_id
AND k.start_time <= s.st
AND s.ed <= k.end_time
GROUP BY s.employee_id, s.st, s.ed
)
SELECT employee_id,
FLOOR(SUM(TIMESTAMPDIFF(SECOND, st, ed)) / 3600) AS total_task_hours,
MAX(concurrent) AS max_concurrent_tasks
FROM covered
GROUP BY employee_id
ORDER BY employee_id;