Person name and their working hours are given.
Return the timeline that tells the time interval and whoever is working during that interval.
Each person is represented by one string entry containing the person name, start time, and end time.
A person is considered working at both startTime and endTime.
Split the full timeline into the smallest non-overlapping time intervals such that the set of working people stays the same throughout each interval.
Return only the intervals where at least one person is working.
If multiple people are working in the same interval, return their names in lexicographical order, separated by ", ".
Consecutive intervals with the same set of working people must be merged into one interval.
Class
WorkingHoursTimeline
Methods
WorkingHoursTimeline()
List<String> getWorkingTimeline(List<String> workingHours)
- Each element in
workingHours is formatted as "name,startTime,endTime".
name is the person's name.
Each person has a unique name.
A person's name never contains the comma character.
startTime is the time when that person starts working.
endTime is the time when that person stops working.
- Return a list of strings formatted as
"startTime to endTime -> name1, name2, ...".
- Each returned interval must contain exactly the people working during the full interval.
- The returned list must be ordered by interval start time.
- Names inside each returned string must be in lexicographical order.
Constraints
1 ≤ workingHours.size() ≤ 10^4
1 ≤ name.length() ≤ 100
- Each
workingHours[i] is formatted as "name,startTime,endTime".
0 ≤ hour < 24
0 ≤ minute < 60
- Each time is in 24-hour
hh:mm format such as 01:00, 05:30, or 14:00.
- Each time value represents a whole minute.
- For every entry,
startTime ≤ endTime.
workingHours never contains null values.
Notes
- A person is considered working for every minute from
startTime through endTime, both inclusive.
- If no one is working in a time range, that range must not appear in the returned list.
- If one person ends at the same time another person starts, both are considered working at that time.
- If
startTime and endTime of a returned interval are equal, that interval represents exactly one minute.
- If multiple people start or end work at the same time, process that boundary correctly and return the resulting active set for that minute and later minutes.
Examples
Example 1
Input:
getWorkingTimeline(workingHours = List.of("Alex,05:00,12:00", "Moris,01:00,11:00", "Rahul,14:00,16:00"))
Output:
List.of("01:00 to 04:59 -> Moris", "05:00 to 11:00 -> Alex, Moris", "11:01 to 12:00 -> Alex", "14:00 to 16:00 -> Rahul")
Explanation:
From 01:00 to 04:59, only Moris is working. From 05:00 to 11:00, both Alex and Moris are working. Since Moris is still working at 11:00, the Alex-only interval starts at 11:01. From 14:00 to 16:00, only Rahul is working.
Example 2
Input:
getWorkingTimeline(workingHours = List.of("Aman,09:00,10:30", "Bhavna,09:00,11:00", "Chirag,10:30,12:00"))
Output:
List.of("09:00 to 10:29 -> Aman, Bhavna", "10:30 to 10:30 -> Aman, Bhavna, Chirag", "10:31 to 11:00 -> Bhavna, Chirag", "11:01 to 12:00 -> Chirag")
Explanation:
Aman and Bhavna both work from 09:00. At 10:30, Aman is still working because end time is inclusive, and Chirag also starts working at 10:30. So 10:30 forms its own one-minute interval with all three people. After that, Aman is no longer working, Bhavna works through 11:00, and Chirag continues until 12:00.
Example 3
Input:
getWorkingTimeline(workingHours = List.of("Nina,08:00,09:00"))
Output:
List.of("08:00 to 09:00 -> Nina")
Explanation:
Only one person is given, so the answer contains exactly one interval, and both the start minute and end minute are included.