Greedy Algorithm Visualizer
Interactive tool to master the Interval Scheduling pattern. Visualize how sorting by end time and selecting non-overlapping intervals works step-by-step.
Interval Scheduling Visualizer
Find maximum non-overlapping intervals (Greedy: Sort by End Time)
status
last end
def interval_scheduling(intervals):
# Sort by end time
intervals.sort(key=lambda x: x[1])
count = 0
last_end = -1
for start, end in intervals:
# If no overlap with last selected
if start >= last_end:
count += 1
last_end = end
# Select this interval
else:
# Skip this interval
pass
return countUnderstanding Interval Scheduling
Interval Scheduling is the quintessential problem for understanding greedy algorithms. The goal is to select the maximum number of non-overlapping intervals. The seemingly simple strategy of “always pick the interval that ends earliest” is guaranteed to produce an optimal solution.
Why Sort by End Time?
Maximizing Future Capacity
By picking the interval that finishes earliest, you leave the maximum possible room for subsequent intervals. This resource maximization is the heart of the greedy choice property.
Other Sorts Fail
Sorting by start time or duration doesn't work. A short interval might overlap with everything, and an early starting interval might run forever. Only end-time sorting works for this problem.
Greedy Choice Property
A problem has the Greedy Choice Property if a global optimal solution can be arrived at by selecting a local optimal (greedy) choice. In Interval Scheduling, selecting the earliest finishing interval is always a safe move that is part of some optimal solution.
How to Use the Visualizer
- Use Next/Previous buttons to step through the algorithm
- Observe the Sort by End Time step first
- Watch the decision making: compare
start_timewithlast_end_time - Green intervals are selected; Red/crossed-out intervals are skipped due to overlap
- Use Auto Play to see the smooth progression of the algorithm
Practice Problems
Apply what you've learned with these LeetCode problems:
Learn More
Explore our comprehensive greedy algorithm tutorials and guides:
Want to Practice LeetCode Smarter?
LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.
Also compatible with Edge, Brave, and Opera
