Count Days Without Meetings Problem
Description
LeetCode Problem 3169.
You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive).
Return the count of days when the employee is available for work but no meetings are scheduled.
Note: The meetings may overlap.
Example 1:
Input: days = 10, meetings = [[5,7],[1,3],[9,10]]
Output: 2
Explanation:
There is no meeting scheduled on the 4^th and 8^th days.
Example 2:
Input: days = 5, meetings = [[2,4],[1,3]]
Output: 1
Explanation:
There is no meeting scheduled on the 5^thday.
Example 3:
Input: days = 6, meetings = [[1,6]]
Output: 0
Explanation:
Meetings are scheduled for all working days.
Constraints:
1 <= days <= 10^91 <= meetings.length <= 10^5meetings[i].length == 21 <= meetings[i][0] <= meetings[i][1] <= days
Solution
Approach: Sorting
Sort the meetings by start day and track last, the latest end day so far. When a meeting starts after last, the days between last and its start are free. Then update last to the larger of itself and the meeting's end. After the loop, the days after last are free too.
Complexity: time O(n log n), space O(log n).
class Solution:
def countDays(self, days: int, meetings: List[List[int]]) -> int:
meetings.sort()
ans = last = 0
for st, ed in meetings:
if last < st:
ans += st - last - 1
last = max(last, ed)
ans += days - last
return ans