3
int smallestChair(vector<vector<int>> ×, int targetFriend) {5
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>6
busy; // departure time as first , index as second7
priority_queue<int, vector<int>, greater<int>>8
free; // min heap of chair index that are unoccupied10
// store friend indexes so that they don't get lost after sorting11
for (int i = 0; i < n; ++i) {12
times[i].push_back(i);14
// Sort according to arrival time15
sort(times.begin(), times.end());17
int new_chair = 0; // chairs alloted till now18
for (int i = 0; i < n; ++i) {19
int arrival = times[i][0]; // pop chairs before arrival20
int leave_time = times[i][1];21
int fr = times[i][2]; // friend index22
// free chairs accordingly23
while (!busy.empty() && busy.top().first <= arrival) {24
// cout << "Chair free " << busy.top().second << endl;25
free.push(busy.top().second);28
// No free chair allot new chair30
// cout << "Alloting new_chair " << new_chair << "to" << fr << endl;31
if (fr == targetFriend) return new_chair;32
busy.push({leave_time, new_chair});36
// cout << "giving chair " << x << "to" << fr << endl;38
if (fr == targetFriend) return x;39
busy.push({leave_time, x});