Greedy Algorithms8 sections · 316 units
Open in Course

Non-overlapping - Walkthrough

Tracing the algorithm

Intervals: [[1,2], [2,3], [3,4], [1,3]]. Sorted by end: [[1,2], [2,3], [1,3], [3,4]]. 1.1. Select [1,2]. Last end = 22.

2.2. [2,3]: start 22 >= 22. Select! Last end = 33.

3.3. [1,3]: start 11 < 33. Conflicts. Skip.

4.4. [3,4]: start 33 >= 33. Select! Last end = 44. Max non-overlapping: 33. Total intervals: 44. Minimum removals: 44 - 33 = 11. The removed interval is [1,3]. Notice touching intervals like [2,3] and [3,4] are allowed. The overlap check is start >= lastEnd.