73 / 75
Add non-overlapping intervals before newInterval, merge overlapping ones, add remaining intervals.
Example:
Input:
intervals=[[1,3],[6,9]], newInterval=[2,5]Output:
[[1,5],[6,9]]Common Mistakes:
- Not handling three phases separately (before, overlap, after)
- Wrong overlap condition (should be start <= newInterval.end)
- Not updating both start and end when merging
- Forgetting to add the merged interval to result
Notes:
Three-phase algorithm. Time O(n), Space O(n) for result. No sorting needed as input already sorted.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Add non-overlapping intervals before newInterval, merge overlapping ones, add remaining intervals.
Example:
Input:
intervals=[[1,3],[6,9]], newInterval=[2,5]Output:
[[1,5],[6,9]]Common Mistakes:
- Not handling three phases separately (before, overlap, after)
- Wrong overlap condition (should be start <= newInterval.end)
- Not updating both start and end when merging
- Forgetting to add the merged interval to result
Notes:
Three-phase algorithm. Time O(n), Space O(n) for result. No sorting needed as input already sorted.
73/75
Insert Interval