Combine time ranges that overlap into the fewest ranges possible.
You have meetings at 1–3, 2–6 and 8–10. The first two overlap, so your calendar really shows one busy block from 1 to 6, plus 8–10. Merging intervals does that for any list.
The trick is to sort by start time first. Then overlapping ranges become neighbors, and one left-to-right sweep is enough:
1-10, 2-3, 4-5. When the new interval is completely inside the last one, why must we use max for the end?1-3, 3-5. They only touch at 3. Does this code merge them?1-2, 4-5, 7-8.cur[1] instead of max(last[1], cur[1]). An interval nested inside another would shrink the result.[1,3] and [3,5] merge depends on <= versus <. Read the problem statement.intervals[0] panics on an empty slice, so check the length first.O(n log n)). The sweep afterwards is a single pass (O(n)).