Maximum Movies You Can Watch (Greedy/Interval Scheduling)

viaGlassdoor

Problem: Given a list of movies each with a start and end time, find the maximum number of non-overlapping movies you can watch.

Constraints: Up to 10^5 movies; times can overlap arbitrarily.

Example: Input: [(1,3),(2,4),(3,5),(6,8)] Output: 3 (e.g., (1,3),(3,5),(6,8))

Approach: Classic activity-selection greedy: sort movies by end time, then iterate, greedily picking a movie if its start time is >= the end time of the last picked movie. This greedy choice (always prefer the interval ending soonest) is provably optimal for maximizing count of non-overlapping intervals, running in O(n log n) due to the sort.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account