PIXELBANKv9.1.0
Menu

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 <= intervals.length <= 10^4
  • intervals are sorted and non-overlapping
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.