non-overlapping-intervals.sh — zsh

Non-overlapping Intervals

medium
arraysintervalsgreedysorting

Given an array of intervals intervals, remove the minimum number of intervals so that the remaining intervals do not overlap. By this problem's convention, intervals that only touch at an endpoint (like [1,2] and [2,3]) are considered non-overlapping and may both stay.

Examples
Example 1
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
Output: 1
Example 2
Input: intervals = [[1,2],[1,2],[1,2]]
Output: 2
Example 3
Input: intervals = [[1,2],[2,3]]
Output: 0
🔒 6 hidden
Running will execute all 9 cases, including 6 hidden ones.