Skip to content

Commit db863bf

Browse files
committed
feat: solve No.1834
1 parent c490fe1 commit db863bf

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# 1834. Single-Threaded CPU
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Sorting, Heap (Priority Queue).
5+
- Similar Questions: Parallel Courses III, Minimum Time to Complete All Tasks.
6+
7+
## Problem
8+
9+
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​​​​​​th`​​​​ task will be available to process at `enqueueTimei` and will take `processingTimei` to finish processing.
10+
11+
You have a single-threaded CPU that can process **at most one** task at a time and will act in the following way:
12+
13+
14+
15+
- If the CPU is idle and there are no available tasks to process, the CPU remains idle.
16+
17+
- 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.
18+
19+
- Once a task is started, the CPU will **process the entire task** without stopping.
20+
21+
- The CPU can finish a task then start a new one instantly.
22+
23+
24+
Return **the order in which the CPU will process the tasks.**
25+
26+
 
27+
Example 1:
28+
29+
```
30+
Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
31+
Output: [0,2,3,1]
32+
Explanation: The events go as follows:
33+
- At time = 1, task 0 is available to process. Available tasks = {0}.
34+
- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
35+
- At time = 2, task 1 is available to process. Available tasks = {1}.
36+
- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
37+
- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
38+
- At time = 4, task 3 is available to process. Available tasks = {1, 3}.
39+
- At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
40+
- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
41+
- At time = 10, the CPU finishes task 1 and becomes idle.
42+
```
43+
44+
Example 2:
45+
46+
```
47+
Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
48+
Output: [4,3,2,0,1]
49+
Explanation: The events go as follows:
50+
- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
51+
- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
52+
- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
53+
- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
54+
- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
55+
- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
56+
- At time = 40, the CPU finishes task 1 and becomes idle.
57+
```
58+
59+
 
60+
**Constraints:**
61+
62+
63+
64+
- `tasks.length == n`
65+
66+
- `1 <= n <= 105`
67+
68+
- `1 <= enqueueTimei, processingTimei <= 109`
69+
70+
71+
72+
## Solution
73+
74+
```javascript
75+
/**
76+
* @param {number[][]} tasks
77+
* @return {number[]}
78+
*/
79+
var getOrder = function(tasks) {
80+
var arr = tasks.map((task, i) => [task[0], task[1], i]).sort((a, b) => a[0] - b[0]);
81+
82+
var queue = new MinPriorityQueue();
83+
var i = 0;
84+
var res = [];
85+
var lastTime = 0;
86+
while (i < arr.length || queue.size()) {
87+
if (!queue.size() && arr[i][0] > lastTime) {
88+
lastTime = arr[i][0];
89+
}
90+
91+
while (arr[i] && arr[i][0] <= lastTime) {
92+
var priority = arr[i][1] + (arr[i][2] / Math.pow(10, `${arr.length}`.length));
93+
queue.enqueue(arr[i], priority);
94+
i++;
95+
}
96+
97+
var task = queue.dequeue().element;
98+
res.push(task[2]);
99+
lastTime += task[1];
100+
}
101+
102+
return res;
103+
};
104+
```
105+
106+
**Explain:**
107+
108+
nope.
109+
110+
**Complexity:**
111+
112+
* Time complexity : O(n).
113+
* Space complexity : O(n).

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy