📘
Insert Interval
MediumIntervals
Given a sorted list of non-overlapping intervals and a new interval, insert and merge if necessary. Return the result.
Input: first line = intervals as start:end comma-separated (or 'none'), second = new interval start:end.
Example:
Input:
1:3,6:9 2:5
Output:
1 5 6 9
Reasoning:
- The given intervals are [1,3] and [6,9], and the new interval is [2,5].
- We compare the new interval with the existing ones and find that it overlaps with [1,3], so we merge them to get [1,5].
- The merged interval [1,5] does not overlap with [6,9], so we keep [6,9] as is.
- The resulting merged intervals are [1,5] and [6,9], which are then formatted as the output: 1 5 and 6 9.
Constraints:
- 0 <= len(intervals) <= 10^4
- 0 <= start <= end <= 10^5
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.