Greedy Algorithms8 sections · 316 units
Open in Course

Interval Overlap Check

The formula

Let me formalize the overlap check you just used. Two intervals [a,b)[a, b) and [c,d)[c, d) overlap if and only if: a<d and c<ba < d \text{ and } c < b

Think about it: they do NOT overlap if one ends before the other starts. That means bcb \leq c or dad \leq a. The negation gives the overlap condition.

For closed intervals [a,b][a, b] and [c,d][c, d], use ada \leq d and cbc \leq b instead. The boundary matters for touching intervals. Common bug: mixing half-open and closed interval semantics.