Skip to content

Commit 67fed68

Browse files
committed
+ problem 621
1 parent 9abb3dd commit 67fed68

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 621. Task Scheduler
2+
Given a characters array `tasks`, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.
3+
4+
However, there is a non-negative integer `n` that represents the cooldown period between two **same tasks** (the same letter in the array), that is that there must be at least `n` units of time between any two same tasks.
5+
6+
Return *the least number of units of times that the CPU will take to finish all the given tasks*.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> tasks = ["A","A","A","B","B","B"], n = 2
11+
<strong>Output:</strong> 8
12+
<strong>Explanation:</strong>
13+
A -> B -> idle -> A -> B -> idle -> A -> B
14+
There is at least 2 units of time between any two same tasks.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> tasks = ["A","A","A","B","B","B"], n = 0
20+
<strong>Output:</strong> 6
21+
<strong>Explanation:</strong> On this case any permutation of size 6 would work since n = 0.
22+
["A","A","A","B","B","B"]
23+
["A","B","A","B","A","B"]
24+
["B","B","B","A","A","A"]
25+
...
26+
And so on.
27+
</pre>
28+
29+
#### Example 3:
30+
<pre>
31+
<strong>Input:</strong> tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
32+
<strong>Output:</strong> 16
33+
<strong>Explanation:</strong>
34+
One possible solution is
35+
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
36+
</pre>
37+
38+
#### Constraints:
39+
* <code>1 <= task.length <= 10<sup>4</sup></code>
40+
* `tasks[i]` is upper-case English letter.
41+
* The integer `n` is in the range `[0, 100]`.
42+
43+
## Solutions (Rust)
44+
45+
### 1. Solution
46+
```Rust
47+
impl Solution {
48+
pub fn least_interval(tasks: Vec<char>, n: i32) -> i32 {
49+
let mut count = [0; 27];
50+
let mut cooldown = [0; 26];
51+
let mut time = 0;
52+
53+
for &task in &tasks {
54+
count[(task as usize) - 65] += 1;
55+
}
56+
57+
for _ in 0..tasks.len() {
58+
let mut min_cooldown = i32::MAX;
59+
let mut next_task = 26;
60+
61+
for i in 0..26 {
62+
if count[i] > 0 {
63+
min_cooldown = min_cooldown.min(cooldown[i]);
64+
}
65+
}
66+
time = time.max(min_cooldown);
67+
68+
for i in 0..26 {
69+
if cooldown[i] <= time && count[i] > count[next_task] {
70+
next_task = i;
71+
}
72+
}
73+
count[next_task] -= 1;
74+
time += 1;
75+
cooldown[next_task] = time + n;
76+
}
77+
78+
time
79+
}
80+
}
81+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 621. 任务调度器
2+
给你一个用字符数组 `tasks` 表示的 CPU 需要执行的任务列表。其中每个字母表示一种不同种类的任务。任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完。在任何一个单位时间,CPU 可以完成一个任务,或者处于待命状态。
3+
4+
然而,两个 **相同种类** 的任务之间必须有长度为整数 `n` 的冷却时间,因此至少有连续 `n` 个单位时间内 CPU 在执行不同的任务,或者在待命状态。
5+
6+
你需要计算完成所有任务所需要的 **最短时间**
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> tasks = ["A","A","A","B","B","B"], n = 2
11+
<strong>输出:</strong> 8
12+
<strong>解释:</strong> A -> B -> (待命) -> A -> B -> (待命) -> A -> B
13+
在本示例中,两个相同类型任务之间必须间隔长度为 n = 2 的冷却时间,而执行一个任务只需要一个单位时间,所以中间出现了(待命)状态。
14+
</pre>
15+
16+
#### 示例 2:
17+
<pre>
18+
<strong>输入:</strong> tasks = ["A","A","A","B","B","B"], n = 0
19+
<strong>输出:</strong> 6
20+
<strong>解释:</strong> 在这种情况下,任何大小为 6 的排列都可以满足要求,因为 n = 0
21+
["A","A","A","B","B","B"]
22+
["A","B","A","B","A","B"]
23+
["B","B","B","A","A","A"]
24+
...
25+
诸如此类
26+
</pre>
27+
28+
#### 示例 3:
29+
<pre>
30+
<strong>输入:</strong> tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
31+
<strong>输出:</strong> 16
32+
<strong>解释:</strong> 一种可能的解决方案是:
33+
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> (待命) -> (待命) -> A -> (待命) -> (待命) -> A
34+
</pre>
35+
36+
#### 提示:
37+
* <code>1 <= task.length <= 10<sup>4</sup></code>
38+
* `tasks[i]` 是大写英文字母
39+
* `n` 的取值范围为 `[0, 100]`
40+
41+
## 题解 (Rust)
42+
43+
### 1. 题解
44+
```Rust
45+
impl Solution {
46+
pub fn least_interval(tasks: Vec<char>, n: i32) -> i32 {
47+
let mut count = [0; 27];
48+
let mut cooldown = [0; 26];
49+
let mut time = 0;
50+
51+
for &task in &tasks {
52+
count[(task as usize) - 65] += 1;
53+
}
54+
55+
for _ in 0..tasks.len() {
56+
let mut min_cooldown = i32::MAX;
57+
let mut next_task = 26;
58+
59+
for i in 0..26 {
60+
if count[i] > 0 {
61+
min_cooldown = min_cooldown.min(cooldown[i]);
62+
}
63+
}
64+
time = time.max(min_cooldown);
65+
66+
for i in 0..26 {
67+
if cooldown[i] <= time && count[i] > count[next_task] {
68+
next_task = i;
69+
}
70+
}
71+
count[next_task] -= 1;
72+
time += 1;
73+
cooldown[next_task] = time + n;
74+
}
75+
76+
time
77+
}
78+
}
79+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
impl Solution {
2+
pub fn least_interval(tasks: Vec<char>, n: i32) -> i32 {
3+
let mut count = [0; 27];
4+
let mut cooldown = [0; 26];
5+
let mut time = 0;
6+
7+
for &task in &tasks {
8+
count[(task as usize) - 65] += 1;
9+
}
10+
11+
for _ in 0..tasks.len() {
12+
let mut min_cooldown = i32::MAX;
13+
let mut next_task = 26;
14+
15+
for i in 0..26 {
16+
if count[i] > 0 {
17+
min_cooldown = min_cooldown.min(cooldown[i]);
18+
}
19+
}
20+
time = time.max(min_cooldown);
21+
22+
for i in 0..26 {
23+
if cooldown[i] <= time && count[i] > count[next_task] {
24+
next_task = i;
25+
}
26+
}
27+
count[next_task] -= 1;
28+
time += 1;
29+
cooldown[next_task] = time + n;
30+
}
31+
32+
time
33+
}
34+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
[609][609l] |[Find Duplicate File in System][609] |![rb]
358358
[611][611l] |[Valid Triangle Number][611] |![rs]
359359
[617][617l] |[Merge Two Binary Trees][617] |![py]
360+
[621][621l] |[Task Scheduler][621] |![rs]
360361
[622][622l] |[Design Circular Queue][622] |![rs]
361362
[623][623l] |[Add One Row to Tree][623] |![py]
362363
[628][628l] |[Maximum Product of Three Numbers][628] |![py]
@@ -1683,6 +1684,7 @@
16831684
[609]:Problemset/0609-Find%20Duplicate%20File%20in%20System/README.md#609-find-duplicate-file-in-system
16841685
[611]:Problemset/0611-Valid%20Triangle%20Number/README.md#611-valid-triangle-number
16851686
[617]:Problemset/0617-Merge%20Two%20Binary%20Trees/README.md#617-merge-two-binary-trees
1687+
[621]:Problemset/0621-Task%20Scheduler/README.md#621-task-scheduler
16861688
[622]:Problemset/0622-Design%20Circular%20Queue/README.md#622-design-circular-queue
16871689
[623]:Problemset/0623-Add%20One%20Row%20to%20Tree/README.md#623-add-one-row-to-tree
16881690
[628]:Problemset/0628-Maximum%20Product%20of%20Three%20Numbers/README.md#628-maximum-product-of-three-numbers
@@ -3009,6 +3011,7 @@
30093011
[609l]:https://leetcode.com/problems/find-duplicate-file-in-system/
30103012
[611l]:https://leetcode.com/problems/valid-triangle-number/
30113013
[617l]:https://leetcode.com/problems/merge-two-binary-trees/
3014+
[621l]:https://leetcode.com/problems/task-scheduler/
30123015
[622l]:https://leetcode.com/problems/design-circular-queue/
30133016
[623l]:https://leetcode.com/problems/add-one-row-to-tree/
30143017
[628l]:https://leetcode.com/problems/maximum-product-of-three-numbers/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
[609][609l] |[在系统中查找重复文件][609] |![rb]
358358
[611][611l] |[有效三角形的个数][611] |![rs]
359359
[617][617l] |[合并二叉树][617] |![py]
360+
[621][621l] |[任务调度器][621] |![rs]
360361
[622][622l] |[设计循环队列][622] |![rs]
361362
[623][623l] |[在二叉树中增加一行][623] |![py]
362363
[628][628l] |[三个数的最大乘积][628] |![py]
@@ -1683,6 +1684,7 @@
16831684
[609]:Problemset/0609-Find%20Duplicate%20File%20in%20System/README_CN.md#609-在系统中查找重复文件
16841685
[611]:Problemset/0611-Valid%20Triangle%20Number/README_CN.md#611-有效三角形的个数
16851686
[617]:Problemset/0617-Merge%20Two%20Binary%20Trees/README_CN.md#617-合并二叉树
1687+
[621]:Problemset/0621-Task%20Scheduler/README_CN.md#621-任务调度器
16861688
[622]:Problemset/0622-Design%20Circular%20Queue/README_CN.md#622-设计循环队列
16871689
[623]:Problemset/0623-Add%20One%20Row%20to%20Tree/README_CN.md#623-在二叉树中增加一行
16881690
[628]:Problemset/0628-Maximum%20Product%20of%20Three%20Numbers/README_CN.md#628-三个数的最大乘积
@@ -3009,6 +3011,7 @@
30093011
[609l]:https://leetcode.cn/problems/find-duplicate-file-in-system/
30103012
[611l]:https://leetcode.cn/problems/valid-triangle-number/
30113013
[617l]:https://leetcode.cn/problems/merge-two-binary-trees/
3014+
[621l]:https://leetcode.cn/problems/task-scheduler/
30123015
[622l]:https://leetcode.cn/problems/design-circular-queue/
30133016
[623l]:https://leetcode.cn/problems/add-one-row-to-tree/
30143017
[628l]:https://leetcode.cn/problems/maximum-product-of-three-numbers/

0 commit comments

Comments
 (0)