Heaps & Priority Queues
Scheduling & Intervals
Master Heap scheduling algorithms: Meeting Rooms II interval triaging, and cooldown-based Task Scheduler logic.
Last Updated: August 2, 2026
•
15 min read
1. Introduction
What are Scheduling & Interval Problems?
Scheduling and Interval Problems require coordinating events (like meetings, computer processes, or printing tasks) that occupy specific spans of time. Heaps are used to monitor active resources dynamically:Why is it Important?
Static lists cannot track overlapping timelines efficiently. A Priority Queue (Min-Heap) allows us to query which room, server, or resource will become vacant first inO(1) time.
Where is it Used?
2. Mental Model: The Hotel Key Rack
Imagine managing meeting rooms in an office:
3. Core Algorithms & Implementations
1. Meeting Rooms II (Minimum Rooms Needed)
Find the minimum number of conference rooms required to hold all meetings.m:m.start >= heap.peek() (the earliest ending meeting is finished), pop the heap (we reuse that room).
- Push m.end onto the heap (representing the room's new availability threshold).
- The final size of the heap is the minimum number of rooms needed.
2. Task Scheduler with Cooldown (LeetCode 621)
Given char tasks (e.g.['A', 'A', 'B']) and cooldown N, find the least CPU intervals to complete all tasks.
4. Visual Timeline: Meeting Rooms Allocation
Trace showing room allocations for intervals [[0, 30], [5, 10], [15, 20]]:
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test chronological order tracking:Common Mistakes
Warning: 1. Forgetting to Sort by Start Time: Attempting to run meeting allocations on unsorted inputs will fail because you might process a later meeting before an earlier one, disrupting room availability calculations.
> 2. Storing Start Times in the Heap: The heap must track end times (when rooms become free), not start times.
7. Summary
O(N log N) time / O(N) space; Task Scheduler is O(T log A) time (where A is alphabet size ≤ 26).8. Quiz
Question 1: What occurs if a meeting's start time exactly equals the earliest end time in the heap?
Answer: We can reuse that room. The conditionstart >= allocator.peek() is met, so we pop the old end time and push the new end time.
Question 2: What is the maximum size of the priority queue in the Task Scheduler algorithm?
Answer: At most26 elements (for English capital letters A-Z). Because the heap size is bounded by a constant, heap operations are virtually O(1) complexity.
Question 3: If meeting intervals are [[1, 5], [5, 10]], how many rooms are needed?
Answer:1. The second meeting starts at 5, which is ≥ the end time of the first meeting, letting us reuse the room.