-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count Days Without Meetings.cpp
50 lines (32 loc) · 1.19 KB
/
Count Days Without Meetings.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
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
if (intervals.size() <= 1) return intervals;
sort(intervals.begin(), intervals.end());
vector<vector<int>> merged;
merged.push_back(intervals[0]);
for (int i = 1; i < intervals.size(); i++) {
if (merged.back()[1] >= intervals[i][0]) {
merged.back()[1] = max(merged.back()[1], intervals[i][1]);
} else {
merged.push_back(intervals[i]);
}
}
return merged;
}
int countDays(int days, vector<vector<int>>& meetings) {
vector<vector<int>> mergedMeetings = merge(meetings);
int freeDays = 0;
int previousEnd = 0;
for ( auto interval : mergedMeetings) {
if (interval[0] > previousEnd + 1) {
freeDays += (interval[0] - previousEnd - 1);
}
previousEnd = interval[1];
}
if (previousEnd < days) {
freeDays += (days - previousEnd);
}
return freeDays;
}
};