Invert Intervals - aloalgo

Invert Intervals

Medium

You are given a list of non-overlapping intervals, intervals, representing covered segments within a larger overall range [minVal, maxVal]. These intervals are sorted by their start times.

Your task is to invert this list of intervals by finding all the gaps (uncovered segments) between them, within the [minVal, maxVal] range.

The output should be a list of intervals representing these gaps, also sorted and non-overlapping.

Example 1

Inputs
intervals = [
  [1, 3],
  [5, 7]
]
minVal = 0
maxVal = 10
Output
[
  [0, 1],
  [3, 5],
  [7, 10]
]
Explanation:

Gaps are before the first interval, between the intervals, and after the last interval within the given range.

Example 2

Inputs
intervals = [
  [0, 10]
]
minVal = 0
maxVal = 10
Output
[
  
]
Explanation:

The intervals completely cover the entire range [0, 10], leaving no gaps.

Example 3

Inputs
intervals = [
  
]
minVal = 0
maxVal = 5
Output
[
  [0, 5]
]
Explanation:

With no intervals provided, the entire [0, 5] range is considered a single gap.

Loading...
Inputs
intervals = [
  [1, 3],
  [5, 7]
]
minVal = 0
maxVal = 10
Output
[
  [0, 1],
  [3, 5],
  [7, 10]
]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!