Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fea22f8

Browse files
committed
Merge pull request #389 from 0xff-dev/1834
Add solution and test-cases for problem 1834
2 parents d7a2aae + 5cc6129 commit fea22f8

File tree

3 files changed

+129
-22
lines changed

3 files changed

+129
-22
lines changed

‎leetcode/1801-1900/1834.Single-Threaded-CPU/README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [1834.Single-Threaded CPU][title]
22

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-
63
## 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.
714

815
**Example 1:**
916

1017
```
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.
1330
```
1431

15-
## 题意
16-
> ...
32+
**Example 2:**
1733

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Single-Threaded CPU
23-
```go
2434
```
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+
```
2646

2747
## 结语
2848

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,86 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import (
4+
"container/heap"
5+
"sort"
6+
)
7+
8+
/*
9+
这道题开始是这样定义的结构体, 想用Pop后,更新costTime,然后在Fix,后面发现Fix的不太对
10+
11+
type task1 struct {
12+
costTime
13+
tasks []task
14+
}
15+
*/
16+
type task struct {
17+
index int
18+
cost []int
19+
}
20+
type taskHeap []task
21+
22+
func (t *taskHeap) Len() int {
23+
return len(*t)
24+
}
25+
26+
func (t *taskHeap) Less(i, j int) bool {
27+
a := (*t)[i]
28+
b := (*t)[j]
29+
if a.cost[1] == b.cost[1] {
30+
return a.index < b.index
31+
}
32+
return a.cost[1] < b.cost[1]
33+
}
34+
35+
func (t *taskHeap) Swap(i, j int) {
36+
(*t)[i], (*t)[j] = (*t)[j], (*t)[i]
37+
}
38+
39+
func (t *taskHeap) Push(x interface{}) {
40+
*t = append(*t, x.(task))
41+
}
42+
43+
func (t *taskHeap) Pop() interface{} {
44+
old := *t
45+
l := len(old)
46+
x := old[l-1]
47+
*t = old[:l-1]
448
return x
549
}
50+
51+
func Solution(tasks [][]int) []int {
52+
tl := len(tasks)
53+
ans := make([]int, tl)
54+
taskList := make([]task, tl)
55+
for i := 0; i < len(tasks); i++ {
56+
taskList[i] = task{index: i, cost: tasks[i]}
57+
}
58+
h := taskHeap{}
59+
sort.Slice(taskList, func(i, j int) bool {
60+
return taskList[i].cost[0] < taskList[j].cost[0]
61+
})
62+
63+
// wait first task
64+
costTime, done, i := 0, 0, 0
65+
for done < tl {
66+
if taskList[done].cost[0] > costTime {
67+
costTime = taskList[done].cost[0]
68+
}
69+
for ; done < tl && taskList[done].cost[0] <= costTime; done++ {
70+
heap.Push(&h, taskList[done])
71+
}
72+
73+
for h.Len() > 0 && done < tl && costTime < taskList[done].cost[0] {
74+
x := heap.Pop(&h).(task)
75+
ans[i] = x.index
76+
i++
77+
costTime += x.cost[1]
78+
}
79+
}
80+
for h.Len() > 0 {
81+
x := heap.Pop(&h).(task)
82+
ans[i] = x.index
83+
i++
84+
}
85+
return ans
86+
}

‎leetcode/1801-1900/1834.Single-Threaded-CPU/Solution_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{
17+
{1, 2}, {2, 4}, {3, 2}, {4, 1},
18+
}, []int{0, 2, 3, 1}},
19+
{"TestCase2", [][]int{
20+
{7, 10}, {7, 12}, {7, 5}, {7, 4}, {7, 2},
21+
}, []int{4, 3, 2, 0, 1}},
22+
{"TestCase3", [][]int{
23+
{19, 13}, {16, 9}, {21, 10}, {32, 25}, {37, 4}, {49, 24}, {2, 15}, {38, 41}, {37, 34}, {33, 6}, {45, 4}, {18, 18}, {46, 39}, {12, 24},
24+
}, []int{6, 1, 2, 9, 4, 10, 0, 11, 5, 13, 3, 8, 12, 7}},
1925
}
2026

2127
// 开始测试
@@ -30,10 +36,10 @@ func TestSolution(t *testing.T) {
3036
}
3137
}
3238

33-
//压力测试
39+
//压力测试
3440
func BenchmarkSolution(b *testing.B) {
3541
}
3642

37-
//使用案列
43+
//使用案列
3844
func ExampleSolution() {
3945
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /