Loading...
Loading...
The computer lab keeps a booking register. Booking i holds the lab from minute start to minute end, and both of these minutes are part of the booking. The lab assistant wants a short list of the periods when the lab is busy.
Two bookings join into one period when they overlap or touch: 1 3 and 3 5 become 1 5. Bookings with a free minute between them stay apart: 1 5 and 6 8 are two periods. Print the busy periods after joining everything that can be joined, sorted by start.
start end for one booking.start end for each busy period, sorted from earliest to latest.Example
Input
4 8 10 1 3 2 6 15 18
Output
1 6 8 10 15 18
1 3 and 2 6 overlap, so they join into 1 6. The other two bookings stay as they are.
Try the problem first. Open one hint at a time.
Bookings that join are easy to find when they sit next to each other. What order helps?
Sort the bookings by start. Keep one current period, starting with the first booking.
If the next booking starts at or before the end of the current period, stretch the end. Otherwise the current period is finished.
When stretching, keep the larger of the two ends, because a booking can sit fully inside the current period. Do not forget to print the last period.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor