-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path24_Count_Days_Without_Meetings.cpp
66 lines (51 loc) · 1.99 KB
/
24_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 3169. Count Days Without Meetings
// You are given an integer days, representing the total number of days in a period, and a list of meetings, where each meetings[i] = [start, end] represents a meeting spanning from day start to day end (both inclusive).
// Your task is to determine the number of days that are not covered by any meeting.
// Constraints & Conditions:
// Meetings are given as an array of intervals [start, end], where:
// 1 ≤ start ≤ end ≤ days
// The total number of days is given as days, where:
// 1 ≤ days ≤ 10^9
// The list of meetings may have overlapping intervals.
// You need to count only the days that do not fall within any meeting interval.
// Input:
// days: An integer representing the total number of days.
// meetings: A list of integer pairs [start, end], representing meetings scheduled over specific days.
// Output:
// Return an integer representing the number of free days (days not covered by any meeting).
// Example:
// Input:
// int days = 10;
// vector<vector<int>> meetings = {{1, 3}, {5, 6}, {2, 8}};
// Processing:
// Meetings cover days 1 to 3, 2 to 8, and 5 to 6.
// The occupied days (merged) are: {1, 2, 3, 4, 5, 6, 7, 8}.
// The free days are: {9, 10}.
// Output:
// 2
class Solution {
#define pii pair<int, int>
public:
int countDays(int days, vector<vector<int>>& meetings) {
vector<pii> time;
for (auto& meeting : meetings) {
time.push_back(make_pair(meeting[0], 1));
time.push_back(make_pair(meeting[1] + 1, 0));
}
sort(time.begin(), time.end());
int overlap = 0;
int count = time[0].first - 1;
for (int i = 0; i < time.size() - 1; ++i) {
if (time[i].second == 0) {
overlap--;
} else {
overlap++;
}
if (overlap == 0) {
count += time[i + 1].first - time[i].first;
}
}
count += days - time.back().first + 1;
return count;
}
};