CSES Restaurant Customers
Author: Michael Cao
In this problem, we're given intervals with distinct start and end points, and we want to find the maximum number of intervals overlapping some point.
Solution
Let's transform the problem by assigning start points the value and end points the value . Then, if we put the start and end points into an array and sort them, the problem becomes: "given an array of values, find the maximum sum at some prefix in the array." This can be done by looping over the array and storing a running sum.
Warning!
Make sure to not store the values in an array
, as the maximum value can be up to , and instead use something like a vector
.
Code
#include <bits/stdc++.h>using namespace std;using ll = long long;using vi = vector<int>;#define pb push_back#define rsz resize#define all(x) begin(x), end(x)#define sz(x) (int)(x).size()