The Restaurant Growth problem asks you to determine the maximum number of customers that are present in a restaurant at the same time, given a list of customer arrival and departure times.
You are given a list of intervals, where each interval [arrive, leave]
represents a customer who arrives at time arrive
and leaves at time leave
. A customer is considered present in the restaurant from arrive
(inclusive) up to but not including leave
.
Constraints:
To solve this problem, let's first consider the brute-force way: for every possible time unit, count how many customers are present. But this is inefficient if the time range is large.
We realize that the only times the number of customers change is when someone arrives or leaves. So, instead of checking every possible moment, we can just process these "events" (arrivals and departures).
The key insight is to treat each arrival as +1 and each departure as -1 to the current count of customers. By sorting all these events by time and processing them in order, we can keep a running count of customers in the restaurant and track the maximum at any point.
This turns the problem from a potentially slow simulation to a much faster scan of sorted events.
Here’s how we can efficiently solve the problem:
arrive
, +1) and one for departure (leave
, -1).This approach is known as the sweep line algorithm and is commonly used for interval overlap problems.
Suppose we have the following intervals:
Step 1: Create Events
Events: (1, +1), (4, -1), (2, +1), (5, -1), (7, +1), (9, -1)
Step 2: Sort Events
Sorted: (1, +1), (2, +1), (4, -1), (5, -1), (7, +1), (9, -1)
Step 3: Sweep Through Events
Brute-force Approach:
This is much more efficient, especially for large N or wide time ranges.
The Restaurant Growth problem is a classic interval overlap problem. The key insight is to process only the times when customers arrive or leave, using a sweep line algorithm. By representing arrivals as +1 and departures as -1, sorting the events, and sweeping through them, we efficiently compute the maximum number of customers present at any time. This approach is elegant, efficient, and scales well with the number of customers.
def restaurantGrowth(intervals):
events = []
for arrive, leave in intervals:
events.append((arrive, 1)) # arrival event
events.append((leave, -1)) # departure event
# Sort events: first by time, then departures before arrivals if same time
events.sort(key=lambda x: (x[0], x[1]))
current = 0
max_customers = 0
for time, change in events:
current += change
max_customers = max(max_customers, current)
return max_customers
#include <vector>
#include <algorithm>
using namespace std;
int restaurantGrowth(vector<vector<int>>& intervals) {
vector<pair<int, int>> events;
for (auto& interval : intervals) {
events.push_back({interval[0], 1}); // arrival
events.push_back({interval[1], -1}); // departure
}
sort(events.begin(), events.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
});
int current = 0, max_customers = 0;
for (auto& e : events) {
current += e.second;
if (current > max_customers) max_customers = current;
}
return max_customers;
}
import java.util.*;
public class Solution {
public int restaurantGrowth(int[][] intervals) {
List<int[]> events = new ArrayList<>();
for (int[] interval : intervals) {
events.add(new int[]{interval[0], 1}); // arrival
events.add(new int[]{interval[1], -1}); // departure
}
events.sort((a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1]) : Integer.compare(a[0], b[0]));
int current = 0, maxCustomers = 0;
for (int[] event : events) {
current += event[1];
maxCustomers = Math.max(maxCustomers, current);
}
return maxCustomers;
}
}
function restaurantGrowth(intervals) {
const events = [];
for (const [arrive, leave] of intervals) {
events.push([arrive, 1]);
events.push([leave, -1]);
}
events.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]);
let current = 0;
let maxCustomers = 0;
for (const [time, change] of events) {
current += change;
if (current > maxCustomers) maxCustomers = current;
}
return maxCustomers;
}