AlgoFrames

FCFS waiting times

Warm-up · Write code · about 10 min

Process i arrives at time arrival[i] and needs the CPU for burst[i] time units. First come, first served runs processes in order of arrival; ties go to the lower index. A process runs to completion once it starts, and the CPU idles when nobody has arrived.

Return an array whose i-th value is the waiting time of process i: the time between its arrival and its start.

Example 1

Input:

[[0,1,2],[5,3,1]]

Output:

[0,4,6]

Example 2

Input:

[[4,0],[2,3]]

Output:

[0,0]

Process 1 arrives first and is done at 3, before process 0 arrives.

Constraints

  • 1 ≤ n ≤ 1,000, where n = arrival.length = burst.length
  • 0 ≤ arrival[i] ≤ 1,000
  • 1 ≤ burst[i] ≤ 100

Learn the idea first: CPU scheduling: FCFS, SJF, priority, round robin, a visual lesson.

More CPU scheduling: FCFS, SJF, priority, round robin problems