Sign In
You are coding as a Guest. Sign in with your RoleNest account to permanently track your streak, earn XP, and climb the Campus Leaderboard!
Sign In with RoleNest
🔥Insert Interval: Calendar MergingMedium
MediumIntervals•Acceptance: 41.8%

Insert Interval: Calendar Merging

Real-World Engineering Context
Conflict-free meeting room booking in Google Calendar, schedule compaction in airline reservations, and memory chunk defragmentation.
You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [start_i, end_i]` sorted in ascending order by `start_i`. You are also given an interval `newInterval = [start, end]`. Insert `newInterval` into `intervals` such that `intervals` is still sorted and contains no overlapping intervals (merge if necessary).

Sample Test Cases

Input: [[[1,3],[6,9]],[2,5]]
Expected: [[1,5],[6,9]]
Input: [[[1,2],[3,5],[6,7],[8,10],[12,16]],[4,8]]
Expected: [[1,2],[3,10],[12,16]]
Input: [[],[5,7]]
Expected: [[5,7]]

Constraints

  • 0 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • newInterval.length == 2