Non-overlapping Intervals

Interval
Medium

Approach

Diagrams

Analysis

Time

O(n log(n))

Space

O(1)
class Solution:
    def eraseOverlapIntervals(self, intervals):
        endOfLastInterval = float('-inf')
        numIntervalsToErase = 0

        intervals.sort(key=lambda interval: interval[1])

        for startOfCurInterval, endOfCurInterval in intervals:
            if startOfCurInterval >= endOfLastInterval:
                endOfLastInterval = endOfCurInterval

            else:
                numIntervalsToErase += 1

        return numIntervalsToErase

Learn

#Greedy

Videos