74 / 75
Sort intervals by start time, merge overlapping ones by extending end time.
Example:
Input:
intervals=[[1,3],[2,6],[8,10],[15,18]]Output:
[[1,6],[8,10],[15,18]]Common Mistakes:
- Not sorting first (assumes sorted input incorrectly)
- Using < instead of <= for overlap check
- Not using Math.max when extending end (misses nested intervals)
- Forgetting to add last interval to result
Notes:
Classic interval problem. Time O(n log n), Space O(n). Foundation for many interval problems.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Sort intervals by start time, merge overlapping ones by extending end time.
Example:
Input:
intervals=[[1,3],[2,6],[8,10],[15,18]]Output:
[[1,6],[8,10],[15,18]]Common Mistakes:
- Not sorting first (assumes sorted input incorrectly)
- Using < instead of <= for overlap check
- Not using Math.max when extending end (misses nested intervals)
- Forgetting to add last interval to result
Notes:
Classic interval problem. Time O(n log n), Space O(n). Foundation for many interval problems.
74/75
Merge Intervals