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