Algorithmsinterval scheduling

Interval Scheduling — Finish Early

TT
Testlaa Team
May 14, 20261 min read

Sort by end time, pick earliest finishing compatible next—max intervals.

Try this in Python

iv = [(1, 3), (2, 5), (4, 6)]
iv.sort(key=lambda x: x[1])
end = -1
c = 0
for s, e in iv:
    if s >= end:
        c += 1
        end = e
print(c)

Key takeaways

  • Weighted version needs DP, not this greedy.
  • Closed/open interval endpoints matter—pick convention once.
  • >= vs > decides touching intervals.

Tags:

GreedyPythonStudents