#3169

Count Days Without Meetings

Medium
ArraySortingArraySorting
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n log n)
Space
O(1)
O(n)
💡

Intuition

Time O(n log n)Space O(n)

The optimal approach involves merging overlapping meetings first, then counting the gaps between these merged intervals. This is efficient because it reduces the number of checks we need to perform.

⚙️

Algorithm

3 steps
  1. 1Step 1: Sort the meetings by their start time.
  2. 2Step 2: Merge overlapping meetings into a new list.
  3. 3Step 3: Count the available days by calculating gaps between merged meetings and the total days.
solution.py20 lines
1# Full working Python code
2
3def count_days_without_meetings(days, meetings):
4    if not meetings:
5        return days
6    meetings.sort()
7    merged = []
8    for start, end in meetings:
9        if not merged or merged[-1][1] < start:
10            merged.append([start, end])
11        else:
12            merged[-1][1] = max(merged[-1][1], end)
13    available_days = 0
14    last_end = 0
15    for start, end in merged:
16        available_days += start - last_end - 1
17        last_end = end
18    available_days += days - last_end
19    return available_days
20

Complexity note: The sorting step takes O(n log n) time, and merging the meetings takes O(n) time. The space complexity is O(n) due to the merged list.

  • 1Merging intervals is crucial to reduce overlaps.
  • 2Counting gaps between meetings efficiently helps find available days.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.