|
| 1 | +# Count Days Without Meetings |
| 2 | + |
| 3 | +You are given a positive integer, `days`, which represents the total number of days an employee is available for work, |
| 4 | +starting from day 1. You are also given a 2D array, `meetings`, where each entry meetings[i] = [starti, endi] indicates |
| 5 | +that a meeting is scheduled from day starti to day endi (both inclusive). |
| 6 | + |
| 7 | +Your task is to count the days when the employee is available for work but has no scheduled meetings. |
| 8 | + |
| 9 | +> Note: The meetings may overlap. |
| 10 | +
|
| 11 | +## Constraints |
| 12 | + |
| 13 | +- 1 <= days <= 10^5 |
| 14 | +- 1 <= meetings.length <= 10^3 |
| 15 | +- meetings.length == 2 |
| 16 | +- 1 <= `meetings[i][0]` <= `meetings[i][1]` <= days |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 26 | +The core idea of this solution is to merge overlapping meetings into continuous intervals to efficiently track the |
| 27 | +occupied days. We begin by sorting the meetings to process them sequentially. As we iterate, we merge overlapping |
| 28 | +meetings while counting the occupied days whenever gaps appear. Finally, subtracting the total occupied days from the |
| 29 | +available days gives the number of free days. |
| 30 | + |
| 31 | +Using the intuition above, we implement the algorithm as follows: |
| 32 | + |
| 33 | +1. First, sort the meetings based on their start time to process them in order. |
| 34 | +2. Initialize a variable, occupied, with 0 to count the days when the employee has scheduled meetings. |
| 35 | +3. Initialize two variables, start and end, with the first meeting’s start and end times. These variables define the |
| 36 | + beginning and end of the merged meeting interval to efficiently track continuously occupied periods. |
| 37 | +4. Iterate through the remaining meetings: |
| 38 | + - If a meeting overlaps with the current merged meeting, extend the end time to merge it into the existing interval. |
| 39 | + - Otherwise, add the days of the merged meeting to occupied as `occupied = occupied + (end - start + 1)`. Then, update |
| 40 | + the start and end for the next interval. |
| 41 | +5. After the loop, add the days of the last merged interval to occupied. |
| 42 | +6. Return the difference between days and occupied (`days−occupied`), representing the number of days when the employee |
| 43 | + is available for work but has no scheduled meetings. |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +### Time Complexity |
| 59 | + |
| 60 | +The algorithm’s time complexity is O(nlogn), where n is the size of the meetings array. This is due to the sorting step, |
| 61 | +which dominates the overall complexity while merging the intervals runs in O(n). |
| 62 | + |
| 63 | +### Space Complexity |
| 64 | + |
| 65 | +The algorithm’s space complexity is constant, O(1). |
0 commit comments