Maximum Non-Overlapping Meetings in a Workday
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests greedy interval scheduling and string parsing: convert "Name: HH:MM-HH:MM" strings into time intervals, then find the largest set of compatible meetings.
Daily Meeting Planner
You are building a personal scheduling assistant. Given a list of meeting request strings, your tool must figure out how many meetings a person can actually attend in a single day — no two meetings may run at the same time.
Input Format
Each meeting is described by a string in this exact format:
"<Title>: <HH:MM>-<HH:MM>"
<Title>is the meeting name (may contain spaces, but never the substring": ").- Times use 24-hour notation, e.g.
"08:30"or"17:45". - The start time is always strictly before the end time.
Task
Implement optimize_schedule(meetings) that accepts this raw list of strings and returns a single integer: the maximum number of meetings that can be attended without any two overlapping.
Overlap rule: two meetings are considered compatible — not overlapping — when one ends at the exact minute the other begins. For example, a meeting ending at 10:00 and one starting at 10:00 may both be attended.
Tie-breaking rule: when two meetings share the same end time, prefer the shorter one (i.e., the one that starts later). This tie-break does not affect the count, but it must be applied consistently to match expected outputs.
Constraints
0 <= len(meetings) <= 1000- All times fall within
00:00–23:59. - Every meeting satisfies
end > start(no zero-length meetings). - Input strings are well-formed; no validation is required.
Examples
Example 1
Input: optimize_schedule(['A: 09:00-10:00', 'B: 09:30-10:30', 'C: 10:00-11:00'])
Output: 2
Hints
Hint 1
Start by converting each time string into a plain integer (minutes since midnight). That turns string comparison into simple arithmetic.
Hint 2
Once you have numeric intervals, think about which meeting to commit to first. Sorting by end time is the classical greedy strategy for maximizing the count of non-overlapping intervals — ask yourself why picking the earliest-finishing meeting is always at least as good as any other choice.
Hint 3
Be careful about the boundary condition: does 'non-overlapping' mean start > last_end or start >= last_end? The example in the problem tells you which one is correct.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Step 1 — Parse strings into numeric intervals
The format "Title: HH:MM-HH:MM" contains colons both in the separator and inside the times, so splitting naively on ":" gives too many pieces. The fix is to split on ": " (colon followed by a space) exactly once, which isolates the title on the left and the time range on the right. The range then splits cleanly on "-". Each HH:MM becomes HH * 60 + MM, an integer in [0, 1439]. Working in minutes keeps all comparisons simple.
Step 2 — Greedy interval scheduling
This is the classic earliest-deadline-first greedy:
- Sort the parsed intervals by end time. Break ties by duration, shortest first (sort key
(end, end - start)). - Walk through the sorted list. Keep a running
last_end, initially0. Accept a meeting wheneverstart >= last_end, then advancelast_end.
Why is this correct? Exchange argument: suppose an optimal solution skips the globally earliest-ending meeting g and picks some other meeting o first. Because end(g) <= end(o), replacing o with g in that solution leaves every subsequent meeting's compatibility unchanged — they all start no earlier than end(o), which is at least end(g). So the modified solution is still valid and has the same size. At least one optimal solution therefore starts with g. Applying the same argument to the remaining subproblem proves greedy is globally optimal by induction.
Boundary condition: the check is start >= last_end, not start > last_end. A meeting that starts at the exact minute another ends is compatible; the sample case depends on this (meetings A 09:00–10:00 and C 10:00–11:00 are both selected, giving 2).
Common pitfalls
- Splitting on bare
":"breaks the parser. - Sorting by start time or by shortest duration both fail: a single short meeting straddling two longer ones would block them both under duration-sort, and one early long meeting blocks everything under start-sort.
- Using strict
>instead of>=drops the count on the provided sample from 2 to 1.
Complexity
- Time: O(N log N) — dominated by sorting; parsing and the greedy scan are both O(N).
- Space: O(N) for the list of parsed dictionaries.
Reference solution
from typing import List
def _parse(meeting_strings: List[str]) -> list:
"""Convert raw strings into dicts with integer start/end in minutes from midnight."""
parsed = []
for item in meeting_strings:
# Split on ": " (with space) at most once so times are kept intact
name, time_range = item.split(": ", 1)
start_str, end_str = time_range.split("-", 1)
sh, sm = map(int, start_str.split(":"))
eh, em = map(int, end_str.split(":"))
parsed.append({"name": name, "start": sh * 60 + sm, "end": eh * 60 + em})
return parsed
def optimize_schedule(meetings: List[str]) -> int:
parsed = _parse(meetings)
# Sort by end time; break ties by shorter duration (later start = smaller end-start)
parsed.sort(key=lambda m: (m["end"], m["end"] - m["start"]))
count = 0
last_end = 0
for m in parsed:
if m["start"] >= last_end: # touching is allowed, not overlapping
count += 1
last_end = m["end"]
return count