Employee Free Time
Find common free intervals across employee schedules — merge all intervals, take the gaps.
4 min read
intervals sorting
Problem#
Each employee has a schedule (a list of non-overlapping busy intervals, sorted). Return the list of common free intervals — times when every employee is available — exclusive of leading and trailing infinities.
Examples#
[[[1,2],[5,6]],[[1,3]],[[4,10]]]→[[3,4]]
Constraints#
- Total intervals up to
5 * 10^4.
Hints#
Hint 1
Flatten all intervals, merge them as in classic merge-intervals; the gaps between consecutive merged blocks are the common free time.
Approach#
Collect every interval into one array, sort by start, merge to produce a maximal “busy” timeline. The complementary gaps are the answer.
Solution#
class Solution {public: vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule) { vector<Interval> all; for (auto& s : schedule) for (auto& it : s) all.push_back(it); sort(all.begin(), all.end(), [](const Interval& a, const Interval& b){ return a.start < b.start; }); vector<Interval> ans; int end = all[0].end; for (size_t i = 1; i < all.size(); ++i) { if (all[i].start > end) { ans.push_back(Interval(end, all[i].start)); end = all[i].end; } else { end = max(end, all[i].end); } } return ans; }};import "sort"
// Interval { Start, End int } assumed defined by LeetCode.
func employeeFreeTime(schedule [][]*Interval) []*Interval { all := []*Interval{} for _, s := range schedule { all = append(all, s...) } sort.Slice(all, func(i, j int) bool { return all[i].Start < all[j].Start }) ans := []*Interval{} end := all[0].End for i := 1; i < len(all); i++ { if all[i].Start > end { ans = append(ans, &Interval{Start: end, End: all[i].Start}) end = all[i].End } else if all[i].End > end { end = all[i].End } } return ans}from typing import List
# class Interval: Start, End assumed defined by LeetCode
class Solution: def employeeFreeTime(self, schedule: 'List[List[Interval]]') -> 'List[Interval]': all_iv = [iv for s in schedule for iv in s] all_iv.sort(key=lambda iv: iv.start) ans = [] end = all_iv[0].end for i in range(1, len(all_iv)): if all_iv[i].start > end: ans.append(Interval(end, all_iv[i].start)) end = all_iv[i].end else: if all_iv[i].end > end: end = all_iv[i].end return ans// Interval { start, end } assumed defined by LeetCode.
var employeeFreeTime = function(schedule) { const all = []; for (const s of schedule) for (const iv of s) all.push(iv); all.sort((a, b) => a.start - b.start); const ans = []; let end = all[0].end; for (let i = 1; i < all.length; i++) { if (all[i].start > end) { ans.push(new Interval(end, all[i].start)); end = all[i].end; } else if (all[i].end > end) { end = all[i].end; } } return ans;};// Interval { start, end } assumed defined by LeetCode.
class Solution { public List<Interval> employeeFreeTime(List<List<Interval>> schedule) { List<Interval> all = new ArrayList<>(); for (List<Interval> s : schedule) all.addAll(s); all.sort((a, b) -> a.start - b.start); List<Interval> ans = new ArrayList<>(); int end = all.get(0).end; for (int i = 1; i < all.size(); i++) { Interval iv = all.get(i); if (iv.start > end) { ans.add(new Interval(end, iv.start)); end = iv.end; } else if (iv.end > end) { end = iv.end; } } return ans; }}// Interval { start, end } assumed defined by LeetCode.declare class Interval { start: number; end: number; constructor(start?: number, end?: number);}
function employeeFreeTime(schedule: Interval[][]): Interval[] { const all: Interval[] = []; for (const s of schedule) for (const iv of s) all.push(iv); all.sort((a, b) => a.start - b.start); const ans: Interval[] = []; let end = all[0].end; for (let i = 1; i < all.length; i++) { if (all[i].start > end) { ans.push(new Interval(end, all[i].start)); end = all[i].end; } else if (all[i].end > end) { end = all[i].end; } } return ans;}Editorial#
Common free time = complement of the union of busy intervals across all employees. Once you merge all intervals into the maximal busy timeline, the gaps between consecutive merged intervals are exactly the common free periods.
Complexity#
- Time: O(n log n).
- Space: O(n).
Concept revision#
Related#