Search⌘ K
AI Features

Solution: Single-Threaded CPU

C# solution for the Single-Threaded CPU problem using the Heaps pattern.

Statement

You are given a list tasks of length n, where each tasks[i] = [enqueueTime_i, processingTime_i] describes a single task. Task i becomes available to run at time enqueueTime_i and requires processingTime_i units of CPU time to complete.

A single threaded CPU can run at most one task at a time. When the CPU becomes idle, it must choose one available task to execute using the following rules:

  1. Select the available task with the smallest processingTime_i.

  2. If multiple available tasks have the same processingTime_i, select the one with the smallest index i.

Once the CPU starts a task, it runs it to completion without preemption. If no tasks are available, the CPU remains idle until the next task becomes available.

Return an array containing the indices of the tasks in the exact order they are processed by the CPU.

Constraints:

  • tasks.length == n

  • 11 \leq n 105 ...