#603
Easy Database Consecutive available seats
Database
65.0% acceptance
Mar 31, 2026
659
80
No description available.
Solution
Pandas
Time O(1)
Space O(1)
# Table: Cinema
#
# +-------------+------+
# | Column Name | Type |
# +-------------+------+
# | seat_id | int |
# | free | bool |
# +-------------+------+
# seat_id is an auto-increment column for this table.
# Each row of this table indicates whether the ith seat is free or not. 1 means free while 0 means occupied.
#
#
#
# Find all the consecutive available seats in the cinema.
#
# Return the result table ordered by seat_id in ascending order.
#
# The test cases are generated so that more than two seats are consecutively available.
#
# The result format is in the following example.
#
# Example 1:
# Input:
# Cinema table:
# +---------+------+
# | seat_id | free |
# +---------+------+
# | 1 | 1 |
# | 2 | 0 |
# | 3 | 1 |
# | 4 | 1 |
# | 5 | 1 |
# +---------+------+
# Output:
# +---------+
# | seat_id |
# +---------+
# | 3 |
# | 4 |
# | 5 |
# +---------+
import pandas as pd
def consecutive_available_seats(cinema: pd.DataFrame) -> pd.DataFrame:
free = cinema[cinema['free'] == 1]
prev_free = free['seat_id'] - 1
next_free = free['seat_id'] + 1
free_set = set(free['seat_id'])
mask = free['seat_id'].apply(lambda x: (x - 1) in free_set or (x + 1) in free_set)
result = free.loc[mask, ['seat_id']].sort_values('seat_id')
return result