Show problems as
Classic problem #3169MediumArraySorting

Count Days Without Meetings

Real-world version: Days the Workshop Stays Free
Tracked in this browser only
Read Count Days Without Meetings on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A workshop takes bookings over days days numbered 1 through days. bookings[i] = [first, last] reserves the workshop for every day from first to last, both ends included. Bookings may overlap, sit inside one another or arrive in any order.

Return how many of the days days carry no booking at all.

Examples

Example 1

Input
days = 12, bookings = [[5, 7], [1, 3], [9, 10]]
Output
4

Days 1 to 3, 5 to 7 and 9 to 10 are reserved. Day 4, day 8, day 11 and day 12 are left.

Example 2

Input
days = 20, bookings = [[5, 10], [6, 8], [7, 7]]
Output
14

The second and third bookings sit wholly inside the first, so only days 5 to 10 are reserved and the other fourteen days stay free.

Example 3

Input
days = 1, bookings = [[1, 1]]
Output
0

The workshop's single day is booked.

Constraints

  • 1 <= days <= 10^9
  • 1 <= bookings.length <= 10^5
  • bookings[i].length == 2
  • 1 <= bookings[i][0] <= days
  • 1 <= bookings[i][1] <= days
  • bookings[i][0] <= bookings[i][1]

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_days(days: int, bookings: list[list[int]]) -> int:
Java
public int countDays(int days, int[][] bookings)