-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1229.cpp
More file actions
27 lines (25 loc) · 978 Bytes
/
Copy path1229.cpp
File metadata and controls
27 lines (25 loc) · 978 Bytes
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
class Solution {
public:
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
sort(slots1.begin(), slots1.end());
sort(slots2.begin(), slots2.end());
int i = 0, j = 0;
while (i < slots1.size() && j < slots2.size()) {
int max_start_time = max(slots1[i][0], slots2[j][0]);
int min_end_time = min(slots1[i][1], slots2[j][1]);
// if max_start_time < min_end_time we found an intersection
if (max_start_time < min_end_time) {
auto diff = min_end_time - max_start_time;
if (diff >= duration) {
return {max_start_time, max_start_time + duration};
}
}
// move the time interval that ends sooner forward
if(slots1[i][1] < slots2[j][1])
i++;
else
j++;
}
return {};
}
};