1834. Single-Threaded CPU
Practice Single-Threaded CPU for free on NudgeCode: Medium, Heap pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Task i is tasks[i] = [enqueueTime, processingTime]: it becomes available at time enqueueTime and takes processingTime units to run. A single CPU works like this:
- If it is idle and no task is available, it waits until the next task arrives.
- If it is idle and tasks are available, it starts the one with the shortest processing time, breaking ties by smaller index.
- Once started, a task runs to completion. The CPU can pick the next task right when the previous one finishes.
Return the task indices in the order the CPU runs them.
Example
tasks = [[1,2],[2,4],[3,2],[4,1]] -> [0,2,3,1]
Constraints
- 1 <= tasks.length <= 10^4
- 1 <= enqueueTime, processingTime <= 10^9