Greedy Algorithms8 sections · 316 units
Open in Course

My Calendar I - Algorithm

Linear vs balanced

The simple approach: store all booked intervals. For each new booking, check against all existing ones. If no overlap, add it. Time: O(n)O(n) per booking where nn is number of existing bookings. For nn total bookings, O(n2)O(n^2) overall. Space: O(n)O(n) to store all bookings.

A better approach uses a balanced tree to store intervals sorted by start time. Binary search finds potential overlaps in O(logn)O(\log n). For now, the simple approach works for most constraints.