CSES Solutions - Restaurant Customers
Last Updated :
06 Jun, 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--;
}
}
return maxCustomers;
}
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.Arrays;
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 or equal to
// the departure time
if (arr[i] <= dep[j]) {
currentCustomers++;
maxCustomers = Math.max(maxCustomers,
currentCustomers);
i++;
}
else {
j++;
currentCustomers--;
}
}
return maxCustomers;
}
public static void main(String[] args)
{
// Sample Input
int N = 3;
int[][] customers
= { { 5, 8 }, { 2, 4 }, { 3, 9 } };
// Call the solve method and print the result
System.out.println(solve(customers, 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)
Similar Reads
Restaurant App using MERN Stack Creating a Restaurant app will cover a lot of features of the MERN stack. In this tutorial, we'll guide you through the process of creating a restaurant application using the MERN stack. The application will allow users to browse through a list of restaurants, view their menus, and add items to a sh
11 min read
Restaurant Recommendation using MERN This article is about Restaurant Recommendations using the MERN (MongoDB, Express.js, React.js, Node.js) stack. This project displays a list of restaurants to the users. Users can apply filters like location, cuisines, and ratings based on filters applied by the user, the user gets recommended speci
9 min read
Customer Segmentation Customer segmentation is one of the strategies for categorizing customers into groups, in that all those with different aspects forming a given group will share some similarities. Demographic, geographic, psychographic, and behavioral characteristics: age, gender, income level, place of residence, l
13 min read
UI Design of a Restaurant Website A Restaurant Website must be colorful and well designed enough to attract customers. A good UI for a restaurant page enhances customer reviews and when ordering online, it is only this thing that helps the business grow, such as pictures of good food, customer feedback options, ready to avail choice
3 min read
Restaurant Recommendation App using MEAN In this article, we'll explore the process of building a restaurant recommendation application using Angular for the client side and Node.js with Express for the server side. This application will allow users to search for restaurants based on location, cuisine, and rating, providing them with a cur
10 min read