Skip to main content
Back to problems
#2494
Hard Database

Merge overlapping events in the same hall

Database
37.2% acceptance
Mar 31, 2026
55
8

No description available.

Solution

Pandas
Time O(n)
Space O(1)
LeetCode
solution.pandas
# 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.

import pandas as pd

def merge_events(hall_events: pd.DataFrame) -> pd.DataFrame:
  hall_events = hall_events.sort_values(['hall_id', 'start_day']).reset_index(drop=True)
  results = []
  for hall_id, group in hall_events.groupby('hall_id'):
    group = group.sort_values('start_day')
    merged_start = group.iloc[0]['start_day']
    merged_end = group.iloc[0]['end_day']
    for _, row in group.iloc[1:].iterrows():
      if row['start_day'] <= merged_end:
        merged_end = max(merged_end, row['end_day'])
      else:
        results.append({'hall_id': hall_id, 'start_day': merged_start, 'end_day': merged_end})
        merged_start = row['start_day']
        merged_end = row['end_day']
    results.append({'hall_id': hall_id, 'start_day': merged_start, 'end_day': merged_end})
  return pd.DataFrame(results)