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)

๐Ÿ› ๏ธ 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

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit