A calendar app shows a "busy" bar above each day. Given the day's events as [start, end] pairs (minutes since midnight, in any order), write mergeIntervals(events), which returns the busy blocks: the smallest list of non-overlapping intervals covering exactly the same time.
Events that overlap or touch (one ends at the minute the next starts) belong to the same busy block.
mergeIntervals([
[540, 600], // 9:00–10:00 standup + planning
[570, 630], // 9:30–10:30 overlaps the first
[630, 660], // 10:30–11:00 touches the merged block
[780, 840], // 13:00–14:00 separate
]);
// [[540, 660], [780, 840]]
mergeIntervals([[60, 120], [0, 30]]); // [[0, 30], [60, 120]]
mergeIntervals([]); // []
start === end), and one event can be fully contained in another.Spotted a mistake or a rights concern? Report an issue