Merge Overlapping Events in the Same Hall Problem
#2494
Description
LeetCode Problem 2494.
Table: HallEvents
+-------------+------+
| Column Name | Type |
+-------------+------+
| hall_id | int |
| start_day | date |
| end_day | date |
+-------------+------+
This table may contain duplicates rows.
Each row of this table indicates the start day and end day of an event and the hall in which the event is held.
Write a solution to merge all the overlapping events that are held in the same hall. Two events overlap if they have at least one day in common.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
HallEvents table:
+---------+------------+------------+
| hall_id | start_day | end_day |
+---------+------------+------------+
| 1 | 2023-01-13 | 2023-01-14 |
| 1 | 2023-01-14 | 2023-01-17 |
| 1 | 2023-01-18 | 2023-01-25 |
| 2 | 2022-12-09 | 2022-12-23 |
| 2 | 2022-12-13 | 2022-12-17 |
| 3 | 2022-12-01 | 2023-01-30 |
+---------+------------+------------+
Output:
+---------+------------+------------+
| hall_id | start_day | end_day |
+---------+------------+------------+
| 1 | 2023-01-13 | 2023-01-17 |
| 1 | 2023-01-18 | 2023-01-25 |
| 2 | 2022-12-09 | 2022-12-23 |
| 3 | 2022-12-01 | 2023-01-30 |
+---------+------------+------------+
Explanation: There are three halls.
Hall 1:
- The two events ["2023-01-13", "2023-01-14"] and ["2023-01-14", "2023-01-17"] overlap. We merge them in one event ["2023-01-13", "2023-01-17"].
- The event ["2023-01-18", "2023-01-25"] does not overlap with any other event, so we leave it as it is.
Hall 2:
- The two events ["2022-12-09", "2022-12-23"] and ["2022-12-13", "2022-12-17"] overlap. We merge them in one event ["2022-12-09", "2022-12-23"].
Hall 3:
- The hall has only one event, so we return it. Note that we only consider the events of each hall separately.
Solution
Approach: Running maximum of the end day
Merging intervals is the classic sort-and-sweep, done here with window functions per hall.
- Order the events of a hall by
start_day, thenend_day. - For each event, take the latest
end_dayamong all earlier events (MAX(end_day)over the rows from the start up to the previous row). This must be a running maximum, not just the previous row's end, because a long event can cover several later short ones. - An event starts a new merged block when it is the first of its hall, or when its
start_dayis later than that running maximum. Starting on the same day as the maximum still overlaps, since the two events share a day, so the test is strict>. - A running sum of these 0/1 flags gives a block number, and
MIN(start_day),MAX(end_day)per(hall_id, block)is the merged event.
Exact duplicate rows fall in the same block, so they collapse naturally.
The Pandas version removes duplicates, sorts, takes a per-hall cummax of the end day shifted by one row, and numbers the blocks with cumsum.
WITH running AS (
SELECT hall_id, start_day, end_day,
MAX(end_day) OVER (
PARTITION BY hall_id
ORDER BY start_day, end_day
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
) AS prev_end -- latest end among earlier events
FROM HallEvents
), flagged AS (
SELECT hall_id, start_day, end_day,
CASE WHEN prev_end IS NULL OR start_day > prev_end THEN 1 ELSE 0 END AS is_new
FROM running
), grouped AS (
SELECT hall_id, start_day, end_day,
SUM(is_new) OVER (PARTITION BY hall_id ORDER BY start_day, end_day) AS grp
FROM flagged
)
SELECT hall_id,
MIN(start_day) AS start_day,
MAX(end_day) AS end_day
FROM grouped
GROUP BY hall_id, grp;