|  | 
| 1 | 1 | # [1834.Single-Threaded CPU][title] | 
| 2 | 2 | 
 | 
| 3 |  | -> [!WARNING|style:flat] | 
| 4 |  | -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) | 
| 5 |  | - | 
| 6 | 3 | ## Description | 
|  | 4 | +You are given `n` tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where `tasks[i] = [enqueueTimei, processingTimei]` means that the i<sup>th</sup> task will be available to process at enqueueTime<sub>i</sub> and will take processingTime<sub>i</sub> to finish processing. | 
|  | 5 | + | 
|  | 6 | +You have a single-threaded CPU that can process **at most one** task at a time and will act in the following way: | 
|  | 7 | + | 
|  | 8 | +- If the CPU is idle and there are no available tasks to process, the CPU remains idle. | 
|  | 9 | +- If the CPU is idle and there are available tasks, the CPU will choose the one with the **shortest processing time**. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index. | 
|  | 10 | +- Once a task is started, the CPU will **process the entire task** without stopping. | 
|  | 11 | +- The CPU can finish a task then start a new one instantly. | 
|  | 12 | + | 
|  | 13 | +Return the order in which the CPU will process the tasks. | 
| 7 | 14 | 
 | 
| 8 | 15 | **Example 1:** | 
| 9 | 16 | 
 | 
| 10 | 17 | ``` | 
| 11 |  | -Input: a = "11", b = "1" | 
| 12 |  | -Output: "100" | 
|  | 18 | +Input: tasks = [[1,2],[2,4],[3,2],[4,1]] | 
|  | 19 | +Output: [0,2,3,1] | 
|  | 20 | +Explanation: The events go as follows:  | 
|  | 21 | +- At time = 1, task 0 is available to process. Available tasks = {0}. | 
|  | 22 | +- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. | 
|  | 23 | +- At time = 2, task 1 is available to process. Available tasks = {1}. | 
|  | 24 | +- At time = 3, task 2 is available to process. Available tasks = {1, 2}. | 
|  | 25 | +- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. | 
|  | 26 | +- At time = 4, task 3 is available to process. Available tasks = {1, 3}. | 
|  | 27 | +- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. | 
|  | 28 | +- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. | 
|  | 29 | +- At time = 10, the CPU finishes task 1 and becomes idle. | 
| 13 | 30 | ``` | 
| 14 | 31 | 
 | 
| 15 |  | -## 题意 | 
| 16 |  | -> ... | 
|  | 32 | +**Example 2:** | 
| 17 | 33 | 
 | 
| 18 |  | -## 题解 | 
| 19 |  | - | 
| 20 |  | -### 思路1 | 
| 21 |  | -> ... | 
| 22 |  | -Single-Threaded CPU | 
| 23 |  | -```go | 
| 24 | 34 | ``` | 
| 25 |  | - | 
|  | 35 | +Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] | 
|  | 36 | +Output: [4,3,2,0,1] | 
|  | 37 | +Explanation: The events go as follows: | 
|  | 38 | +- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. | 
|  | 39 | +- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. | 
|  | 40 | +- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. | 
|  | 41 | +- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. | 
|  | 42 | +- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. | 
|  | 43 | +- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. | 
|  | 44 | +- At time = 40, the CPU finishes task 1 and becomes idle. | 
|  | 45 | +``` | 
| 26 | 46 | 
 | 
| 27 | 47 | ## 结语 | 
| 28 | 48 | 
 | 
|  | 
0 commit comments