CSES Solutions – Restaurant Customers
Last Updated :
15 Apr, 2024
You are given the arrival and leaving times of N customers in a restaurant as array customers[][], such that customer[i][0] is the arrival time and customer[i][1] is the leaving time. What was the maximum number of customers in the restaurant at any time? You may assume that all arrival and leaving times are distinct.
Note: A customer is not counted to be inside the restaurant at any time >= leaving time and < arrival time.
Examples:
Input: N = 3, customer[][] = {{5, 8}, {2, 4}, {3, 9}}
Output: 2
Explanation: Two customers are present in the restaurant at time = 3, 5, 6 and 7.
Input: N = 4, customer[][] = {{1, 2}, {2, 3}, {3, 5}, {4, 5}}
Output: 2
Explanation: Two customers are present in the restaurant at time = 4.
Approach: To solve the problem, follow the below idea:
The problem can be solved by sorting both the arrival times and the departure times separately in ascending order. Now, we can maintain two pointers to mark the arrival and departure of customers. We can start from the first index of both the arrays and compare the arrival and departure, if the arrival time is lesser so increment the number of customers and move to the next arrival time. Otherwise, decrement the number of customers and move to the next departure time. After iterating over both the arrays, return the maximum customers at any point of time.
Step-by-step algorithm:
- Sort the arrival and departure times in ascending order.
- Maintain two pointers, say i and j to maintain the current position of arrival and departure arrays.
- Maintain a variable, say currentCustomers to keep track of the number of customers in the shop.
- If arr[i] < dep[j], it means that one customer will arrive in the shop before a customer leaves, so we will increase the number of customers in the shop and increment i by 1.
- Else if arr[i] >= dep[i], it means that one customer will leave the shop before a customer arrives, so we will decrease the number of customers in the shop and increment j by 1.
- Store the maximum of currentCustomers as the final answer.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
int solve(vector<vector<int> >& customers, int N)
{
// Store the arrival and departure time in two different
// arrays
int arr[N], dep[N];
for (int i = 0; i < N; i++) {
arr[i] = customers[i][0];
dep[i] = customers[i][1];
}
// Sort the arrival and departure time in ascending
// order
sort(arr, arr + N);
sort(dep, dep + N);
int i = 0, j = 0;
// Variables to store the number of customers in the
// shop and the maximum customers so far
int currentCustomers = 0, maxCustomers = 0;
while (i < N && j < N) {
// If the arrival time is less than the departure
// time
if (arr[i] < dep[j]) {
currentCustomers++;
maxCustomers
= max(maxCustomers, currentCustomers);
i++;
}
// If the arrival time is greater than the departure
// time
else {
j++;
currentCustomers--;
}
}
}
int main()
{
// Sample Input
int N = 3;
vector<vector<int> > customers
= { { 5, 8 }, { 2, 4 }, { 3, 9 } };
cout << solve(customers, N) << endl;
}
Java
import java.util.*;
public class Main {
static int solve(int[][] customers, int N) {
// Store the arrival and departure time in two different arrays
int[] arr = new int[N];
int[] dep = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = customers[i][0];
dep[i] = customers[i][1];
}
// Sort the arrival and departure time in ascending order
Arrays.sort(arr);
Arrays.sort(dep);
int i = 0, j = 0;
// Variables to store the number of customers in the shop and the maximum customers so far
int currentCustomers = 0, maxCustomers = 0;
while (i < N && j < N) {
// If the arrival time is less than the departure time
if (arr[i] < dep[j]) {
currentCustomers++;
maxCustomers = Math.max(maxCustomers, currentCustomers);
i++;
}
// If the arrival time is greater than the departure time
else {
j++;
currentCustomers--;
}
}
return maxCustomers;
}
public static void main(String[] args) {
// Sample Input
int N = 3;
int[][] customers = { { 5, 8 }, { 2, 4 }, { 3, 9 } };
System.out.println(N);
}
}
Python
def solve(customers):
# Create separate lists for arrival and departure times
arrival = sorted([i[0] for i in customers])
departure = sorted([i[1] for i in customers])
n = len(arrival)
i = 0
j = 0
current_customers = 0
max_customers = 0
# Traverse both arrays and update max_customers
while i < n and j < n:
# Customer arrives before or exactly at the departure of
# the previous customer
if arrival[i] <= departure[j]:
current_customers += 1
i += 1
# Customer arrives after the departure of the previous customer
else:
j += 1
current_customers -= 1
max_customers = max(max_customers, current_customers)
return max_customers
# Sample Input
customers = [[5, 8], [2, 4], [3, 9]]
print(solve(customers))
JavaScript
function solve(customers, N) {
// Store the arrival and departure time in two different arrays
let arr = new Array(N);
let dep = new Array(N);
for (let i = 0; i < N; i++) {
arr[i] = customers[i][0];
dep[i] = customers[i][1];
}
// Sort the arrival and departure time in ascending order
arr.sort((a, b) => a - b);
dep.sort((a, b) => a - b);
let i = 0, j = 0;
// Variables to store the number of customers in the shop and the maximum customers so far
let currentCustomers = 0, maxCustomers = 0;
while (i < N && j < N) {
// If the arrival time is less than the departure time
if (arr[i] < dep[j]) {
currentCustomers++;
maxCustomers = Math.max(maxCustomers, currentCustomers);
i++;
}
// If the arrival time is greater than the departure time
else {
j++;
currentCustomers--;
}
}
return maxCustomers;
}
// Sample Input
let N = 4;
let customers = [ [ 1, 2 ], [ 2, 3 ], [ 3, 5 ], [ 4, 5 ] ];
console.log(solve(customers, N));
Time Complexity: O(N * logN), where N is the size of input array customers[].
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...