Find the First Free Slot After Busy Minutes in Text Schedules
Greedy & IntervalsAuto-graded · 26 test casesLast updated
Parse text schedule entries into inclusive minute intervals, merge them, and find the earliest k-minute free slot that follows the first busy minute of the day.
Find the First Free Slot After Busy Minutes in Text Schedules
A group of people must all attend a single meeting that lasts k minutes. Their commitments are given as a list of text records. Each record has four fields separated by single spaces: a person's name, an activity, a start time and an end time. For example: "Sam sleep 02:00 10:00".
Return the earliest time the meeting can begin, formatted as "HH:MM", or "-1" if no valid start exists on that day.
Rules
- The day runs from minute
00:00to minute23:59. A record with startaand endbmarks every minute fromatobas busy, including both endpoints. For instance,"Lilly exercise 12:00 12:59"blocks exactly 60 minutes. - A minute is free if no record covers it. Records may overlap, whether they belong to the same person or to different people. Names and activities have no effect on the result.
- A meeting starting at minute
suses minutessthroughs + k - 1. Every one of those minutes must be free, and the final one must be no later than23:59. - The meeting must start after the first busy minute of the day. Free time before the earliest busy minute is not eligible.
- Return the earliest valid start as
"HH:MM"with a two-digit hour and a two-digit minute, or"-1"if there is none.
Constraints
1 <= len(schedules) <= 10^4- Each record has exactly four fields separated by single spaces. The name and the activity are nonempty strings of English letters.
- Both times are valid zero-padded
HH:MMvalues from00:00to23:59, and the start time is not later than the end time. 1 <= k <= 1440
Examples
Example 1
Input: first_free_slot(['Sam sleep 02:00 10:00', 'Lilly exercise 12:00 12:59'], 90)
Output: '10:01'
Example 2
Input: first_free_slot(['Sam sleep 02:00 10:00', 'Lilly exercise 12:00 12:59'], 180)
Output: '13:00'
Example 3
Input: first_free_slot(['Ann work 08:00 11:00', 'Bo gym 10:30 12:00', 'Cy call 12:30 23:30'], 30)
Output: '-1'
Hints
Hint 1
Convert each HH:MM into a minute number from 0 to 1439. Names and activities can be ignored entirely, so each record becomes one closed interval [a, b].
Hint 2
Because both endpoints are busy, intervals like [600, 700] and [701, 800] leave no free minute between them. Sort and merge whenever the next start is at most the current end plus one.
Hint 3
Since the meeting must come after the first busy minute, the only candidate starts are the minute right after a merged block ends. Check each gap in order, using 1440 as the end for the last block, and stop at the first gap with at least k free minutes.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Turn each
HH:MMinto a minute index from 0 to 1439. Each record becomes a closed interval[a, b]; the name and activity are irrelevant because everyone must attend. - Sort the intervals and merge them into disjoint busy blocks. Since endpoints are inclusive, two intervals that merely touch (for example
[600, 700]and[701, 800]) leave no free minute between them, so merge whena <= last_end + 1, and keep the larger of the two ends. - Because the meeting must start after the first busy minute, the only usable free time is in the gaps that follow a busy block: between one block and the next, or after the last block until the end of the day.
- For a block ending at
end, the gap begins atend + 1. Letnxtbe the next block's start, or1440for the last block. The gap holdsnxt - (end + 1)free minutes. Scan gaps in time order and returnend + 1(formatted) for the first gap with at leastkminutes. - If no gap is large enough, return
"-1".
Why it is correct
- Any valid start is free and later than the first busy minute, so it lies in the gap right after some merged block.
- Within a gap, if any start works then the gap has at least
kminutes, so its first minuteend + 1also works and is the earliest in that gap. - Merged blocks are sorted, so the first qualifying gap yields the globally earliest start.
- Using
1440as the sentinel for the last gap enforces the rule that the meeting must finish by23:59. A block ending at23:59yields a gap of 0. - Free time before the first block is never examined, which satisfies the "after the first busy minute" rule.
Complexity
- Time: O(n log n) for sorting; parsing, merging and scanning are O(n).
- Space: O(n) for the intervals and merged blocks.
Reference solution
from typing import List
def first_free_slot(entries: List[str], k: int) -> str:
def to_min(t: str) -> int:
return int(t[:2]) * 60 + int(t[3:])
intervals = []
for entry in entries:
parts = entry.split(" ")
intervals.append((to_min(parts[2]), to_min(parts[3])))
intervals.sort()
merged = []
for a, b in intervals:
if merged and a <= merged[-1][1] + 1:
if b > merged[-1][1]:
merged[-1][1] = b
else:
merged.append([a, b])
for i, (_, end) in enumerate(merged):
start = end + 1
nxt = merged[i + 1][0] if i + 1 < len(merged) else 1440
if nxt - start >= k:
return "%02d:%02d" % (start // 60, start % 60)
return "-1"