DSA sheet / Sorting and searching

Restaurant Customers

medium about 20 min sweep-linesorting
Open on CSES

Do these first: Distinct Numbers

The problem in brief

You get n customers (up to 200 000), each with an arrival time and a leaving time (up to 10^9). Print the maximum number of customers present at the same moment.

Try it

Tab indents; press Esc, then Tab, to leave the box. Ctrl+Enter runs.

Hints

Stuck? Reveal one hint at a time. Each nudges without giving away the next.

The walkthrough

Spoilers ahead: open a section only after you have given the hints a fair try.

How to think

The brute force checks, for every interval, how many others overlap it: O(n^2). The insight that beats it is that a snapshot of “how many people are here” is a step function that only changes at event times. So instead of intervals, think of a stream of +1 and -1 events. The peak of the step function is the answer, and finding the peak of a step function is just a running sum over sorted events.

This is the sweep-line technique: convert objects with extent into point events, sort, and maintain state as you sweep. It generalises to overlapping rectangles, meeting rooms and skyline problems. The habit: when a question is about “at the same time”, ask which finite set of moments can matter, and sweep over exactly those.

Intuition

Stand at the door with a clicker. Every arrival is a click up, every departure a click down. Read out the events in time order; the highest number the clicker ever shows is the answer.

Approach
  1. For each customer create two events: (arrival, +1) and (leaving, -1).
  2. Sort all events by time; on equal times put the -1 before the +1 (treat the interval as ending just before its leaving time, so a leaver and an arriver at the same instant do not overlap).
  3. Sweep: add each event’s delta to a counter and keep the maximum.
  4. Print the maximum.

The tie rule is the one place a correct-looking sweep can be off by one: this solution treats a leaver and an arriver at the same instant as not overlapping. Pitfalls: 2n events, up to 400 000 items to sort; times fit in 32 bits, and the counter never exceeds n.

Complexity

O(n log n) for the sort, O(n) for the sweep; O(n) memory.

Solutions

Written from scratch and checked by compiling and running each one against a brute force on random inputs. Fast input/output, the way you would submit it.

Show solutions (C++17, Python 3, Java 17, Node.js)
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<pair<int, int>> ev;
    ev.reserve(2 * n);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        ev.push_back({a, +1});
        ev.push_back({b, -1});  // (t, -1) sorts before (t, +1): leavers go first on ties
    }
    sort(ev.begin(), ev.end());
    int cur = 0, best = 0;
    for (auto &e : ev) {
        cur += e.second;
        best = max(best, cur);
    }
    cout << best << "\n";
    return 0;
}

Problem: CSES Problem Set (Antti Laaksonen, University of Helsinki), CC BY-NC-SA 4.0. The explanations and code above are original (© Anupam Kumar).

Related problems

Back to the sheet