Say you’re given a start and end time.
Also you’re given an array of jobs, described by their start and end times. These jobs may overlap (ie, there can be multiple jobs running at once). I need to find a way to determine how much time was spent idle and not running any job.
Of course, if only one job can be running at any time, I could just subtract out the running times of each job, but the overlap part has me stumped.
Put all the start and end times into a single array, tagging them as either start or end times. Sort the array by time. Now iterate through the sorted list, keeping a count of how many jobs are running by:
Whenever you increment from zero to one, add (current_time – previous_time) to the total idle time. Remember to also special case the start and end if necessary.