Find Overlapping Shifts II Problem
Description
LeetCode Problem 3268.
Table: EmployeeShifts
+------------------+----------+
| Column Name | Type |
+------------------+----------+
| employee_id | int |
| start_time | datetime |
| end_time | datetime |
+------------------+----------+
(employee_id, start_time) is the unique key for this table.
This table contains information about the shifts worked by employees, including the start time, and end time.
Write a solution to analyze overlapping shifts for each employee. Two shifts are considered overlapping if they occur on the same date and one shift's end_time is later than another shift's start_time.
For each employee, calculate the following:
- The maximum number of shifts that overlap at any given time.
- The total duration of all overlaps in minutes.
Return the result table ordered by employee_id in ascending order.
The query result format is in the following example.
Example:
Input:
EmployeeShifts table:
+-------------+---------------------+---------------------+
| employee_id | start_time | end_time |
+-------------+---------------------+---------------------+
| 1 | 2023-10-01 09:00:00 | 2023-10-01 17:00:00 |
| 1 | 2023-10-01 15:00:00 | 2023-10-01 23:00:00 |
| 1 | 2023-10-01 16:00:00 | 2023-10-02 00:00:00 |
| 2 | 2023-10-01 09:00:00 | 2023-10-01 17:00:00 |
| 2 | 2023-10-01 11:00:00 | 2023-10-01 19:00:00 |
| 3 | 2023-10-01 09:00:00 | 2023-10-01 17:00:00 |
+-------------+---------------------+---------------------+
Output:
+-------------+---------------------------+------------------------+
| employee_id | max_overlapping_shifts | total_overlap_duration |
+-------------+---------------------------+------------------------+
| 1 | 3 | 600 |
| 2 | 2 | 360 |
| 3 | 1 | 0 |
+-------------+---------------------------+------------------------+
Explanation:
- Employee 1 has 3 shifts:
- 2023-10-01 09:00:00 to 2023-10-01 17:00:00
- 2023-10-01 15:00:00 to 2023-10-01 23:00:00
- 2023-10-01 16:00:00 to 2023-10-02 00:00:00
The maximum number of overlapping shifts is 3 (from 16:00 to 17:00). The total overlap duration is: - 2 hours (15:00-17:00) between 1st and 2nd shifts - 1 hour (16:00-17:00) between 1st and 3rd shifts - 7 hours (16:00-23:00) between 2nd and 3rd shifts Total: 10 hours = 600 minutes
- Employee 2 has 2 shifts:
- 2023-10-01 09:00:00 to 2023-10-01 17:00:00
- 2023-10-01 11:00:00 to 2023-10-01 19:00:00
The maximum number of overlapping shifts is 2. The total overlap duration is 6 hours (11:00-17:00) = 360 minutes.
- Employee 3 has only 1 shift, so there are no overlaps.
The output table contains the employee_id, the maximum number of simultaneous overlaps, and the total overlap duration in minutes for each employee, ordered by employee_id in ascending order.
Solution
Approach: Time slices and a self-join
The two numbers need different tools.
Peak concurrency. Collect every distinct start and end time of an employee. Two consecutive time points (found with LEAD) bound an elementary slice in which the set of running shifts does not change. A shift covers a slice when start_time <= slice start and end_time >= slice end, so counting the covering shifts per slice gives the concurrency, and the maximum over the slices is the peak. A shift that ends exactly when another starts never covers a slice together with it, so back-to-back shifts are not counted as overlapping.
Total overlap minutes. Self-join the shifts of the same employee. For the pair a, b with a.start_time < b.start_time < a.end_time (this ordering counts each overlap once), the overlap runs from b.start_time to the earlier of the two end times. Summing those lengths over all pairs gives the total; in the example the pairwise overlaps of 120, 60 and 420 minutes add up to 600. The sum is NULL when there is no pair, so the last join is a LEFT JOIN with COALESCE(..., 0).
The Pandas version does the same with a sweep: each shift becomes a +1 event at its start and a -1 event at its end, events are sorted with ends before starts at equal times, and the peak is the maximum running sum. The overlap total comes from merging the table with itself on employee_id.
WITH points AS (
-- every distinct start or end time of an employee's shifts
SELECT employee_id, start_time AS t FROM EmployeeShifts
UNION
SELECT employee_id, end_time FROM EmployeeShifts
),
slices AS (
-- consecutive time points form the elementary slices [t, next_t)
SELECT employee_id, t, LEAD(t) OVER (PARTITION BY employee_id ORDER BY t) AS next_t
FROM points
),
concurrency AS (
-- how many shifts fully cover each slice
SELECT s.employee_id, COUNT(*) AS cnt
FROM slices AS s
JOIN EmployeeShifts AS e
ON e.employee_id = s.employee_id
AND e.start_time <= s.t
AND e.end_time >= s.next_t
WHERE s.next_t IS NOT NULL
GROUP BY s.employee_id, s.t
),
peak AS (
SELECT employee_id, MAX(cnt) AS max_overlapping_shifts
FROM concurrency
GROUP BY employee_id
),
overlap AS (
-- a.start < b.start < a.end: the overlap runs from b.start to the earlier end
SELECT a.employee_id,
SUM(TIMESTAMPDIFF(MINUTE, b.start_time, LEAST(a.end_time, b.end_time))) AS total_overlap_duration
FROM EmployeeShifts AS a
JOIN EmployeeShifts AS b
ON a.employee_id = b.employee_id
AND a.start_time < b.start_time
AND a.end_time > b.start_time
GROUP BY a.employee_id
)
SELECT p.employee_id,
p.max_overlapping_shifts,
COALESCE(o.total_overlap_duration, 0) AS total_overlap_duration
FROM peak AS p
LEFT JOIN overlap AS o ON o.employee_id = p.employee_id
ORDER BY p.employee_id;