|
| 1 | +from typing import List |
| 2 | + |
| 3 | + |
| 4 | +def count_days(days: int, meetings: List[List[int]]) -> int: |
| 5 | + """ |
| 6 | + Counts the number of days the employee is available for work but has no scheduled meetings. |
| 7 | +
|
| 8 | + Complexity: |
| 9 | +
|
| 10 | + Standard time complexity for sorting is O(n log(n)). The loop is O(n). The part that dominates the overall time |
| 11 | + complexity is the sorting and the loop. It amounts to O(n log(n)) as the overall. The overall space complexity for |
| 12 | + this approach is O(1) without accounting for the in place sorting that is taking place which is O(n) using timsort |
| 13 | + in Python. |
| 14 | +
|
| 15 | + Summary of Performance |
| 16 | + --- |
| 17 | + Metric Complexity Reason |
| 18 | + --- |
| 19 | + Time O(nlogn) Dominated by the initial sort of n meetings. |
| 20 | + Space O(n) Required by Timsort for internal temporary storage. |
| 21 | +
|
| 22 | + Args: |
| 23 | + days (int): The total number of days the employee is available for work |
| 24 | + meetings (List[List[int]]): A list of meetings, where each meeting is represented as a list of two integers [start, end] |
| 25 | + Returns: |
| 26 | + int: The number of days the employee is available for work but has no scheduled meetings |
| 27 | + """ |
| 28 | + # sort meetings by start in place, incurs O(n log(n)) time complexity |
| 29 | + meetings.sort(key=lambda x: x[0]) |
| 30 | + |
| 31 | + # keep track of free days |
| 32 | + free_days = 0 |
| 33 | + |
| 34 | + # a pointer that keeps track of the current day |
| 35 | + last_busy_day = 0 |
| 36 | + |
| 37 | + # iterate through the meetings, for each meeting we might have a gap |
| 38 | + for meeting in meetings: |
| 39 | + # get the start and end of the meeting |
| 40 | + start, end = meeting |
| 41 | + |
| 42 | + # calculate gaps, if the meeting starts at start and our last_busy_day is less than start - 1, the days in |
| 43 | + # between are free |
| 44 | + if start > last_busy_day + 1: |
| 45 | + free_days += (start - 1) - last_busy_day |
| 46 | + |
| 47 | + # update the last busy day to the maximum of the last busy day and the end of the meeting |
| 48 | + |
| 49 | + # This ensures that if a meeting is completely contained within a previous busy block, the boundary doesn't move |
| 50 | + # backward, which handles overlaps perfectly. |
| 51 | + last_busy_day = max(last_busy_day, end) |
| 52 | + |
| 53 | + # add the remaining days to the free days |
| 54 | + return free_days + (days - last_busy_day) |
| 55 | + |
| 56 | + |
| 57 | +def count_days_2(days: int, meetings: List[List[int]]) -> int: |
| 58 | + """ |
| 59 | + Counts the number of days the employee is available for work but has no scheduled meetings. |
| 60 | + |
| 61 | + This implementation merges overlapping meetings and counts total occupied days. |
| 62 | + |
| 63 | + Time Complexity: O(n log n) due to sorting |
| 64 | + Space Complexity: O(1) excluding sort overhead |
| 65 | + |
| 66 | + Args: |
| 67 | + days (int): The total number of days the employee is available for work |
| 68 | + meetings (List[List[int]]): A list of meetings, where each meeting is represented as a list of two integers [start, end] |
| 69 | + Note: This function modifies the input list by sorting it in place. |
| 70 | + Returns: |
| 71 | + int: The number of days the employee is available for work but has no scheduled meetings |
| 72 | + """ |
| 73 | + # Sort the meetings based on their start time to process them in order |
| 74 | + meetings.sort() |
| 75 | + |
| 76 | + # Initialize a variable with 0 to count the number of days when the employee has meetings scheduled |
| 77 | + occupied = 0 |
| 78 | + |
| 79 | + # Initialize two variables with the first meeting’s start and end times |
| 80 | + # Sort the meetings based on their start time to process them in order |
| 81 | + meetings.sort() |
| 82 | + |
| 83 | + # Handle edge case of empty meetings |
| 84 | + if not meetings: |
| 85 | + return days |
| 86 | + |
| 87 | + # Initialize a variable with 0 to count the number of days when the employee has meetings scheduled |
| 88 | + occupied = 0 |
| 89 | + |
| 90 | + # Initialize two variables with the first meeting's start and end times |
| 91 | + start, end = meetings[0] |
| 92 | + |
| 93 | + # Iterate through the remaining meetings |
| 94 | + for i in range(1, len(meetings)): |
| 95 | + # If a meeting overlaps with the current merged meeting |
| 96 | + if meetings[i][0] <= end: |
| 97 | + # Extend the end time to merge it |
| 98 | + end = max(end, meetings[i][1]) |
| 99 | + else: |
| 100 | + # Add the days of the merged meeting |
| 101 | + occupied += end - start + 1 |
| 102 | + |
| 103 | + # Update start and end for the next interval |
| 104 | + start, end = meetings[i] |
| 105 | + |
| 106 | + # Add the days of the last merged meeting |
| 107 | + occupied += end - start + 1 |
| 108 | + |
| 109 | + # Return the free days |
| 110 | + return days - occupied |
0 commit comments