Run Jobs Shortest-First and Report Each Job's Wait
Implement sjfWaitingTimes
A single processor must run a set of jobs. Job i becomes available at time arrival[i] and needs burst[i] time units. Once a job starts it runs to completion. Whenever the processor becomes free it starts the available job with the smallest burst (ties go to the earlier arrival, then the smaller index); if no job is available it idles until the next one arrives.
A job's wait is the time between its arrival and the moment it starts. Return the wait of every job, in the original input order.
Scanning every job each time the processor frees up works but repeats effort. Sorting the jobs by arrival and keeping the already-arrived jobs in a min-heap ordered by burst hands over the shortest available job in O(log n).
Example 1:
Input: arrival = [0,2,3,6], burst = [7,4,1,3]
Output: [0,9,4,2]
Example 2:
Input: arrival = [5,6], burst = [2,1]
Output: [0,1]
Example 3:
Input: arrival = [0], burst = [4]
Output: [0]
+ 10 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ arrival.length ≤ 30, and burst.length equals arrival.length - ●
0 ≤ arrival[i] ≤ 100, 1 ≤ burst[i] ≤ 50; job i becomes available at time arrival[i] and needs burst[i] time units on a single processor - ●
The processor runs one job at a time and never interrupts it (non-preemptive). Whenever it becomes free, it starts the available job with the smallest burst; ties go to the earlier arrival, then to the smaller index - ●
If no job is available when the processor becomes free, it idles until the next arrival. A job's wait is (its start time) − (its arrival time). Return every job's wait, in input order
arrival =
burst =