Single-Threaded CPU
You have a single-threaded CPU and n tasks, where tasks[i] = [enqueueTime_i, processingTime_i]. A task becomes available at its enqueue time. When the CPU is free it must, from the tasks that have already become available, pick the one with the smallest processing time (breaking ties by the smaller task index), run it to completion without interruption, then immediately become free again. If no task is available the CPU idles until the next one arrives. Return the order in which the tasks are processed, as a list of their original indices.
Open official problem prompt ↗Reproduce the exact execution order of a greedy single-threaded scheduler: whenever the CPU is free, it runs the shortest available task (smallest index on ties), and we report which task index runs at each step.
Think of a single doctor in a walk-in clinic. Patients arrive at different times and sit in the waiting room. When the doctor finishes with someone, they don't take whoever arrived first — they take the patient whose visit will be quickest (and if two would take equally long, the one with the lower ticket number). If the room is empty, the doctor waits for the next arrival.
- Input
- tasks = [[1,2],[2,4],[3,2],[4,1]]
- Output
- [0, 2, 3, 1]
- Why
- Task 0 arrives at t=1 and runs t=1..3; by t=3 tasks 1 and 2 are available, task 2 has the smaller processing time (2<4) so it runs t=3..5; then task 3 (proc 1) beats task 1 (proc 4) and runs t=5..6; finally task 1 runs.
tasks.length == n1 <= n <= 10^51 <= enqueueTime_i, processingTime_i <= 10^9