v9.1.1
Menu

Non-overlapping Intervals

mediumFREEIntervalsBlind 75

Given intervals [start, end], return the minimum number of intervals to remove to make the rest non-overlapping.

Input: intervals as start:end comma-separated.

Example

Input
1:2,2:3,3:4,1:3
Output
1
Reasoning
  • The given intervals are [1,2], [2,3], [3,4], and [1,3], which can be sorted by their end values as [1,2], [2,3], [3,4], and [1,3].
  • After sorting, we can see that [1,3] overlaps with [1,2] and [2,3], but if we remove [1,3], the remaining intervals are non-overlapping.
  • The number of intervals to remove is 11, as removing [1,3] makes the rest non-overlapping.
  • The final output is 11, which is the minimum number of intervals to remove.

Constraints

  • 1 <= len(intervals) <= 10^5
  • -5 * 10^4 <= start < end <= 5 * 10^4

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.