Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Sort intervals by END time (this is the key greedy insight). Keep the first interval. For each subsequent interval, if it starts before the previous end, it overlaps and must be removed. Otherwise, keep it and update the end boundary.
Sorting by end time and always keeping the interval that ends earliest leaves maximum room for future intervals. This greedy choice is optimal because an interval with an earlier end time can never be worse than one with a later end time — it blocks fewer future intervals.
Updated Feb 2026