MEDIUM Intervals
251. Employee Free Time
๐ Problem
We are given a list schedule, where schedule[i] = [starti, endi, i] for the ith employee. Return the list of finite intervals representing free time for all employees. Our definition of a free interval is: [start, previous end] or [end, starti + 1] of the schedule. However, though our representation of intervals counts meeting times at discrete points, there are no 2 available intervals between [starti, endi] and [startj, endj] because of the exclusivity of the end times.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
๐ก Approach
- โ Flatten all employee intervals into one list
- โ Sort all intervals by start time
- โ Merge overlapping intervals
- โ Return merged intervals
- โ Time: O(n log n), Space: O(n)
๐งญ Prerequisites
๐ ๏ธ Hints & Pitfalls
Hints
- โขFlatten all employee intervals into one list
- โขSort all intervals by start time
- โขMerge overlapping intervals
Common Pitfalls
- โขReturn merged intervals
- โขTime: O(n log n), Space: O(n)
๐งช Test Cases
Test Case 1
Not run Input:
employeeFreeTime(schedule: number[][]); Expected:
Computed from hidden reference Test Case 2
Not run Input:
employeeFreeTime([[[1,2],[5,6]],[[1,3]],[[4,10]]); Expected:
Computed from hidden reference Test Case 3
Not run Input:
employeeFreeTime([[[1,3],[6,7]],[[2,4]],[[2,5],[9,12]]); Expected:
Computed from hidden reference ๐ Code Editor
๐ค Output