188. Employee Schedule Common Free Time

Asked in

Employee Schedule Common Free Time
You are given the work schedule of multiple employees.

Each employee has one or more working time intervals. For every employee, the intervals are non-overlapping and sorted by start time.

Your task is to return all finite intervals where every employee is free at the same time.

Only positive-length free time intervals should be returned. For example, an interval like 5,5 must not be included because its length is zero.

Infinite free time before the first working interval or after the last working interval must not be included in the answer.

Method Signature

List<String> employeeFreeTime(List<String> schedule)
  • schedule contains one string for each employee.
  • Each employee string contains that employee's working intervals.
  • Each interval is written as start,end.
  • Multiple intervals for the same employee are separated by |.
  • For example, "1,2|5,6" means the employee works during intervals [1,2) and [5,6).
    start is included and end is excluded.
  • The returned list must contain common free intervals in sorted order.
  • Each returned interval must also be written as start,end.
  • You must never use null as a parameter value.

Interval Rules

  • Intervals are closed-open for the purpose of measuring free time boundaries.
  • If one working interval ends at time x and the next working interval starts at time x, there is no positive-length free time between them.
  • Only intervals with start < end should be returned.
  • Free time intervals containing negative infinity or positive infinity must be ignored.

Constraints

  • 1 ≤ schedule.size() ≤ 50
  • Each employee has at least one working interval.
  • Each employee has at most 50 working intervals.
  • 0 ≤ start < end ≤ 10^8
  • For each employee, working intervals are sorted by start time.
  • For each employee, working intervals do not overlap.

Examples

Example 1

Input: employeeFreeTime(schedule = ["1,3|6,7", "2,4", "2,5|9,12"])
Output: ["5,6", "7,9"]
Explanation: All employees are busy from 1 to 5, then free from 5 to 6. They are busy again from 6 to 7, then free from 7 to 9. Infinite free time before 1 and after 12 is not returned.

Example 2

Input: employeeFreeTime(schedule = ["1,2|5,6", "1,3", "4,10"])
Output: ["3,4"]
Explanation: The common finite free intervals are checked between the merged busy intervals. The only positive-length finite interval when all employees are free is 3,4.

Example 3

Input: employeeFreeTime(schedule = ["1,5", "5,8", "8,10"])
Output: []
Explanation: The busy intervals touch each other at their endpoints. Intervals like 5,5 and 8,8 have zero length, so they are not returned.

Example 4

Input: employeeFreeTime(schedule = ["10,20", "12,15", "18,25"])
Output: []
Explanation: At least one employee is working continuously from 10 to 25, so there is no finite positive-length common free time.




Please use Laptop/Desktop or any other large screen to add/edit code.