Greedy Algorithms8 sections · 316 units
Open in Course

Remove Covered - Implementation

The code

function removeCoveredIntervals(intervals)
 sort intervals by (start ascending, end descending)
 count := 0
 maxEnd := 0
 for each interval in intervals
 if interval.end > maxEnd then
 count := count + 1
 maxEnd := interval.end
 return count

Time: O(nlogn)O(n \log n). Space: O(1)O(1) extra.