Skip to content

Commit 01a1b67

Browse files
aQuaaQua
aQua
authored and
aQua
committed
621 added
1 parent bc42a40 commit 01a1b67

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [621. Task Scheduler](https://leetcode.com/problems/task-scheduler/)
2+
3+
## 题目
4+
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
5+
6+
However, there is a non-negative cooling interval `n` that means between two `same tasks`, there must be at least n intervals that CPU are doing different tasks or just be idle.
7+
8+
You need to return the `least` number of intervals the CPU will take to finish all the given tasks.
9+
10+
Example 1:
11+
```
12+
Input: tasks = ['A','A','A','B','B','B'], n = 2
13+
Output: 8
14+
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
15+
```
16+
17+
Note:
18+
1. The number of tasks is in the range [1, 10000].
19+
1. The integer n is in the range [0, 100].
20+
21+
## 解题思路
22+
23+
见程序注释
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Problem0621
2+
3+
func leastInterval(tasks []byte, n int) int {
4+
5+
return 8
6+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package Problem0621
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
tasks []byte
18+
n int
19+
}
20+
21+
// ans 是答案
22+
type ans struct {
23+
one int
24+
}
25+
26+
func Test_Problem0621(t *testing.T) {
27+
ast := assert.New(t)
28+
29+
qs := []question{
30+
31+
question{
32+
para{
33+
[]byte{'A', 'A', 'A', 'B', 'B', 'B'},
34+
2,
35+
},
36+
ans{
37+
8,
38+
},
39+
},
40+
41+
// 如需多个测试,可以复制上方元素。
42+
}
43+
44+
for _, q := range qs {
45+
a, p := q.ans, q.para
46+
fmt.Printf("~~%v~~\n", p)
47+
48+
ast.Equal(a.one, leastInterval(p.tasks, p.n), "输入:%v", p)
49+
}
50+
}

0 commit comments

Comments
 (0)