1942.cpp (824B)
1 class Solution { 2 public: 3 int smallestChair(const vector<vector<int>> ×, int targetFriend) const { 4 static tuple<int, bool, int> timeline[20002]; 5 const int n = size(times); 6 priority_queue<int> seats; 7 8 for (int i = 0; i < n; i++) { 9 timeline[i * 2] = {times[i][0], true, i}; 10 timeline[i * 2 + 1] = {times[i][1], false, i}; 11 seats.push(-i); 12 } 13 14 sort(timeline, timeline + n * 2); 15 16 static int assign[10001]; 17 for (const auto [time, sit, person] : span(timeline, timeline + n * 2)) { 18 if (person == targetFriend) return -seats.top(); 19 if (sit) 20 assign[person] = seats.top(), seats.pop(); 21 else 22 seats.push(assign[person]); 23 } 24 25 return -1; 26 } 27 };