Imagine you're a calendar app developer who needs to simplify a user's busy schedule. Given an array of time intervals where each interval is represented as [start, end], your task is to merge all overlapping intervals and return a clean, non-overlapping schedule.
For example, if someone has meetings from [1,3] and [2,6], these overlap and should be merged into a single interval [1,6]. Your goal is to take a messy schedule with potentially overlapping time slots and return the minimum number of intervals that cover the same time periods.
Key Points:
- Two intervals
[a,b]and[c,d]overlap ifb โฅ c(they share at least one point) - The result should contain no overlapping intervals
- The order of intervals in the result doesn't matter
Input & Output
Visualization
Time & Space Complexity
In worst case, we need O(n) iterations, each checking O(n) pairs, with O(n) time to merge
Only using constant extra space for temporary variables
Constraints
- 1 โค intervals.length โค 104
- intervals[i].length == 2
- 0 โค starti โค endi โค 104
- All intervals are valid (start โค end)