Loading problem...
You are given a collection of time intervals represented as an array intervals, where each interval intervals[i] = [startᵢ, endᵢ] defines a closed range from startᵢ to endᵢ.
Your objective is to determine the minimum number of intervals that must be removed from the collection so that all remaining intervals are mutually exclusive (i.e., no two intervals share any common time points except possibly their endpoints).
Two intervals are considered non-overlapping if they share at most a single endpoint. For instance, intervals [1, 5] and [5, 10] are non-overlapping because they only touch at point 5, whereas intervals [1, 5] and [4, 8] overlap because they share points 4 and 5.
Your task is to find the optimal selection strategy that maximizes the number of intervals kept, which equivalently minimizes the number of intervals removed.
intervals = [[1,2],[2,3],[3,4],[1,3]]1The interval [1,3] overlaps with both [1,2] and [2,3]. By removing [1,3], the remaining intervals [1,2], [2,3], and [3,4] are all mutually non-overlapping (adjacent endpoints are allowed). This is the minimum removal required.
intervals = [[1,2],[1,2],[1,2]]2All three intervals are identical and therefore overlap with each other. We must keep only one interval, so we need to remove 2 intervals to achieve a non-overlapping set.
intervals = [[1,2],[2,3]]0The two intervals share only the endpoint 2, which is considered non-overlapping by our definition. No removals are necessary since they are already mutually exclusive.
Constraints