Algorithm for finding the total downtime of a given array of start / end time

Say you have been given a start and end time.

You are also given an array of tasks described by their start and end times. These tasks may overlap (i.e., several tasks may be performed simultaneously). I need to find a way to determine how much time was spent on downtime and no work was done.

Of course, if only one task can be started at any time, I could just subtract the time of each task, but part of the overlap puzzled me.

+5
source share
5 answers

, . . , :

, , (current_time - previous_time) . , .

+6

, .
- , 1 .
, 1.
, , 0.

+12

. .

int, . , . . , . .

, . , 15:00 17:00, , 3- 4- , , .

, , . , .

, , . ( ) . , . , , .

+3
Sort the jobs based on their end times.

Jobs<startTime,EndTime>[] = contains the Sorted list.

Take first Job in the list
Jobs[0]

intialize:
    EndTime = Job[0].EndTime
    StartTime = Job[0].StartTime
     idleTime =0;

For i = 1 till Jobs.size
{
    if ( Jobs[i].EndTime >= StartTime )
    {
        //Do nothing, its overlapping
    }
    else
    { //Previoys Job time doesnot overlap, so get the idle time.
        idleTime += (StartTime -Jobs[i].EndTime);
    }

     StartTime = Jobs[i].StartTime;
     EndTime = Jobs[i].EndTime;
}
0

, . - . :

idle_time = 0
sort jobs by start time
foreach job in jobs
{
  if (job.start > current_chunk.end)
  {
    idle_time += job.start - current_chunk.end
  }
  if (job.end > current_chunk.end)
  {
    current_chunk.end = job.end
  }
}

. .

0
source

All Articles