3169. Count Days Without Meetings LeetCode Solution

In this guide, you will get 3169. Count Days Without Meetings LeetCode Solution with the best time and space complexity. The solution to Count Days Without Meetings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Days Without Meetings solution in C++
  4. Count Days Without Meetings solution in Java
  5. Count Days Without Meetings solution in Python
  6. Additional Resources
3169. Count Days Without Meetings LeetCode Solution image

Problem Statement of Count Days Without Meetings

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 4th and 8th days.

Example 2:

Input: days = 5, meetings = [[2,4],[1,3]]
Output: 1
Explanation:
There is no meeting scheduled on the 5th day.

Example 3:

Input: days = 6, meetings = [[1,6]]
Output: 0
Explanation:
Meetings are scheduled for all working days.

Constraints:

1 <= days <= 109
1 <= meetings.length <= 105
meetings[i].length == 2
1 <= meetings[i][0] <= meetings[i][1] <= days

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(\texttt{sort})
See also  2429. Minimize XOR LeetCode Solution

3169. Count Days Without Meetings LeetCode Solution in C++

class Solution {
 public:
  int countDays(int days, vector<vector<int>>& meetings) {
    int freeDays = 0;
    int prevEnd = 0;

    ranges::sort(meetings);

    for (const vector<int>& meeting : meetings) {
      const int start = meeting[0];
      const int end = meeting[1];
      if (start > prevEnd)
        freeDays += start - prevEnd - 1;
      prevEnd = max(prevEnd, end);
    }

    return freeDays + max(0, days - prevEnd);
  }
};
/* code provided by PROGIEZ */

3169. Count Days Without Meetings LeetCode Solution in Java

class Solution {
  public int countDays(int days, int[][] meetings) {
    int freeDays = 0;
    int prevEnd = 0;

    Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));

    for (int[] meeting : meetings) {
      final int start = meeting[0];
      final int end = meeting[1];
      if (start > prevEnd)
        freeDays += start - prevEnd - 1;
      prevEnd = Math.max(prevEnd, end);
    }

    return freeDays + Math.max(0, days - prevEnd);
  }
}
// code provided by PROGIEZ

3169. Count Days Without Meetings LeetCode Solution in Python

class Solution:
  def countDays(self, days: int, meetings: list[list[int]]) -> int:
    freeDays = 0
    prevEnd = 0

    for start, end in sorted(meetings):
      if start > prevEnd:
        freeDays += start - prevEnd - 1
      prevEnd = max(prevEnd, end)

    return freeDays + max(0, days - prevEnd)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.