Pick One Day Per Workshop Within Its Availability Window
Greedy & IntervalsAuto-graded · 20 test casesLast updated
Choose a distinct day for as many workshops as possible, each within its own window, using an earliest-deadline-first greedy with a min-heap.
Workshop Scheduling
You are given a list of workshops. Each workshop is described by a pair [open_day, close_day], the first and last day on which it can be attended (both inclusive).
Attending a workshop takes exactly one day. You may choose any single day d with open_day <= d <= close_day to attend it. You can attend at most one workshop per day, and each workshop can be attended at most once.
Return the maximum number of workshops you can attend.
Function
Implement max_workshops(windows), where windows is a list of [open_day, close_day] pairs. It returns an integer.
Clarifications
- Attending a workshop does not occupy its whole window. It only uses the one day you pick inside the window.
- Several workshops may share the same window, or even the same single day. Only one of them can use any given day.
- Days are positive integers. You may choose any day in a window, so days with no workshops are simply unused.
- The order of the input list carries no meaning.
Constraints
1 <= len(windows) <= 10^5windows[i] = [open_day, close_day]1 <= open_day <= close_day <= 10^5
Examples
Example 1
Input: max_workshops([[1, 2], [2, 3], [3, 4]])
Output: 3
Example 2
Input: max_workshops([[1, 2], [1, 2], [1, 2]])
Output: 2
Example 3
Input: max_workshops([[1, 1], [1, 1], [2, 2]])
Output: 2
Hints
Hint 1
Think day by day. On a given day, several workshops may be available. Which one is the most urgent to attend right now?
Hint 2
Among the workshops that have opened and not yet been attended, the one with the earliest close day has the fewest remaining chances, so attend that one first. A min-heap of close days supports this.
Hint 3
Sort windows by open day. On each day, push all newly opened workshops into the heap, discard those whose close day is before today, then pop one to attend. If the heap is empty, jump straight to the next open day instead of stepping through empty days.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Process days in increasing order and always attend the available workshop that closes soonest (earliest-deadline-first).
- Sort the workshops by open day.
- Keep a min-heap of the close days of workshops that have opened but have not been attended.
- For the current day
d:- Push the close day of every workshop with
open_day <= d. - Pop every close day
< d. Those workshops can no longer be attended. - If the heap is not empty, pop the smallest close day, count one attendance, and move to day
d + 1.
- Push the close day of every workshop with
- If the heap is empty, jump directly to the next workshop's open day. This keeps the work proportional to the number of workshops rather than the number of days.
Why it is correct
Use an exchange argument. Suppose on day d the greedy attends workshop e, which has the earliest close day among the available ones. Take an optimal schedule that agrees with the greedy on all earlier days.
- If the optimal schedule attends a different workshop
fon dayd, andeis attended later on dayd', swap them. This is valid becaused' <= close(e) <= close(f), andfis already open on dayd. Ifeis never attended, replacefwithe, which keeps the count the same. - If the optimal schedule attends nothing on day
d, addinge(or moving it to dayd) is never worse.
So some optimal schedule matches the greedy through day d, and by induction the greedy is optimal. Leaving a day idle when something is available can never help.
Complexity
- Time:
O(n log n). Sorting costsO(n log n), and each workshop is pushed and popped at most once atO(log n)each. Idle gaps are skipped in one step. - Space:
O(n)for the heap and the sorted copy.
Reference solution
import heapq
from typing import List
def max_workshops(windows: List[List[int]]) -> int:
items = sorted(windows) # by open day, then close day
heap = [] # close days of opened, unattended workshops
attended = 0
i = 0
day = 0
n = len(items)
while i < n or heap:
# Nothing available: jump to the next opening day.
if not heap:
day = items[i][0]
# Make every workshop that has opened by today available.
while i < n and items[i][0] <= day:
heapq.heappush(heap, items[i][1])
i += 1
# Drop workshops whose window has already closed.
while heap and heap[0] < day:
heapq.heappop(heap)
# Attend the available workshop with the earliest close day.
if heap:
heapq.heappop(heap)
attended += 1
day += 1
return attended