Booked minutes: hints, no solution

Easy · Intervals

A clinic room is in use whenever at least one appointment is running. Appointments are given as [start, end) pairs of minutes counted from the start of the booking calendar, in no particular order. They may overlap, sit inside each other or repeat.

Return the total number of minutes during which the room is in use, counting each minute once no matter how many appointments cover it. An appointment from 60 to 90 covers 30 minutes, and one that starts exactly when another ends adds no overlap.

Examples

Input
appointments = [[60, 120], [90, 150], [200, 230]]
Output
120
Note
Minutes 60 to 150 are covered once each, giving 90, and 200 to 230 adds 30.
Input
appointments = [[0, 10], [10, 20]]
Output
20
Note
The two appointments touch but do not overlap, so their 10 minutes each add up.
Input
appointments = []
Output
0
Note
No appointments means no minutes.

Constraints

  • 0 <= appointments.length <= 80000
  • 0 <= start < end <= 10000000 for every appointment
  • Appointments may overlap, nest or repeat

Hint 1: a nudge

If two appointments overlap, part of their lengths is counted twice by simple addition. What if you could treat overlapping appointments as a single longer stretch first?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first