Leetcode 3169: Count Days Without Meetings

grid47
grid47
Exploring patterns and algorithms
Dec 26, 2023 7 min read

You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). Additionally, you are given a 2D array meetings, where meetings[i] = [start_i, end_i] represents the start and end days (inclusive) of the ith meeting. Your task is to determine the number of days when the employee is available for work, but no meetings are scheduled.
Problem
Approach
Steps
Complexity
Input: The input consists of an integer `days`, representing the total number of days the employee is available for work, and a 2D array `meetings` where each element contains two integers, `start_i` and `end_i`, representing the start and end days of each meeting.
Example: Example 1: Input: days = 10, meetings = [[5,7],[1,3],[9,10]] Output: 2 Explanation: The employee has no meetings scheduled on days 4 and 8.
Constraints:
• 1 <= days <= 10^9
• 1 <= meetings.length <= 10^5
• 1 <= meetings[i][0] <= meetings[i][1] <= days
Output: Return an integer representing the number of days the employee is available for work, with no meetings scheduled.
Example: Example 1: Input: days = 10, meetings = [[5,7],[1,3],[9,10]] Output: 2
Constraints:
• The output will be a non-negative integer.
Goal: The goal is to calculate the number of days where the employee is available for work, excluding the days with scheduled meetings.
Steps:
• Sort the meeting intervals based on the start day.
• Iterate through the meetings and merge overlapping or adjacent meetings.
• Calculate the total number of days occupied by meetings.
• Subtract the total number of days occupied by meetings from the total available days to find the number of days the employee is free.
Goal: The input should satisfy the following constraints.
Steps:
• days is a positive integer, up to 10^9.
• The number of meetings is between 1 and 10^5.
• Each meeting is represented by two integers, where 1 <= start <= end <= days.
Assumptions:
• The meetings may overlap, so they need to be merged.
• The meetings are scheduled between day 1 and the given number of days.
Input: Example 1:
Explanation: For `days = 10` and `meetings = [[5,7], [1,3], [9,10]]`, after merging the meetings, the days occupied are from day 1 to day 3 and day 5 to day 7, and day 9 to day 10. The employee is available on days 4 and 8, so the output is 2.

Input: Example 2:
Explanation: For `days = 5` and `meetings = [[2,4], [1,3]]`, after merging the meetings, the days occupied are from day 1 to day 4. The employee is available on day 5, so the output is 1.

Input: Example 3:
Explanation: For `days = 6` and `meetings = [[1,6]]`, the meeting occupies all 6 days. Therefore, the employee is not available on any day, so the output is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus