-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
70 lines (59 loc) · 1.89 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int smallestChair(vector<vector<int>>& times, int targetFriend) {
// n friends from [0..n-1] attending.
// infinite chairs.
// times[i] represent {arrivalTime, departureTime} of friend i
// Not sorted.
const size_t n = times.size();
// Instead of having an entire array of indices, just store the
// targetArrivalTime. The question guarantees unique arrivalTimes.
const int targetArrivalTime = times[targetFriend][0];
// Allows efficient lookup of the minimum numbered chair available.
priority_queue<int, vector<int>, greater<int>> availableChairs;
for (int i = 0; i < n; ++i) {
availableChairs.push(i);
}
sort(times.begin(), times.end());
// {time, chairIdx}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>>
departureQueue;
// Determine when to reclaim chairs.
for (const auto time : times) {
const int currTime = time[0];
// Reclaim chairs first
while (!departureQueue.empty() &&
departureQueue.top().first <= currTime) {
const int reclaimIdx = departureQueue.top().second;
departureQueue.pop();
availableChairs.push(reclaimIdx);
}
// Unless input is wrong, there will always be an available chair.
// No need to check for empty
const int chairIdx = availableChairs.top();
availableChairs.pop();
if (currTime == targetArrivalTime) {
return chairIdx;
}
const int departureTime = time[1];
departureQueue.emplace(departureTime, chairIdx);
}
return -1;
}
};