Restaurant Customers
Explanation
In this problem, n
customers enter and leave a store at different times. We want to find the maximum number of customers in the store at any given time.
This can be done by storing the entering times as a tuple/pair of (enter_time, 1)
and the leaving times as a tuple/pair of (leave_time, -1)
. Then, we sort the list of tuples/pairs by the first element of each tuple/pair. This way, we can iterate through the list of tuples/pairs and keep track of the number of customers in the store at any given time.
Code
import sys
input = sys.stdin.readline
customers = []
for _ in range(int(input())):
enter, leave = map(int, input().split())
customers.append((enter, 1))
customers.append((leave, -1))
customers.sort()
count = 0
max_count = 0
for _, delta in customers:
count += delta
max_count = max(max_count, count)
print(max_count)