diff --git a/README.md b/README.md index f787ec8..2f77fd0 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,7 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 | 581 |[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/581.%20Shortest%20Unsorted%20Continuous%20Subarray.md)|Easy| | | 605 |[Can Place Flowers](https://leetcode.com/problems/can-place-flowers)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/605.%20Can%20Place%20Flowers.md)|Easy| | | 617 |[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[C++](solutions/617.%20Merge%20Two%20Binary%20Trees.md)|Easy| | +| 621 |[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[C++](solutions/621.%20Task%20Scheduler.md)|Medium| | | 628 |[Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/628.%20Maximum%20Product%20of%20Three%20Numbers.md)|Easy| | | 643 |[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/643.%20Maximum%20Average%20Subarray%20I.md)|Easy| | | 661 |[Image Smoother](https://leetcode.com/problems/image-smoother)|[C++](https://github.com/ShusenTang/LeetCode/blob/master/solutions/661.%20Image%20Smoother.md)|Easy| | diff --git a/solutions/621. Task Scheduler.md b/solutions/621. Task Scheduler.md new file mode 100644 index 0000000..a3507cf --- /dev/null +++ b/solutions/621. Task Scheduler.md @@ -0,0 +1,84 @@ +# [621. Task Scheduler](https://leetcode.com/problems/task-scheduler/) + +# 思路 + +## 思路一、暴力模拟 + +首先应该明确我们始终**应该优先处理出现次数最多的任务**。为此我们可以模拟这个过程,先统计每个字母的次数,然后将这些次数送入到一个优先队列(最大堆)里,然后每一轮都从优先队列里面取`n+1`个元素出来将其次数减一,不断重复这个过程直到队列空。 + +设任务总数为N,不同的任务数为M(M<=26),那么时间复杂度为O(NlogM),空间复杂度为O(M)。亲测此方法500ms左右,效率较低。 + +## 思路二 + +还是按照优先处理出现次数最多的任务这个原则。我们假设 A 为出现次数最多的任务,假设其出现了 p 次,考虑到冷却时间,那么执行完所有任务的时间至少为 `(p - 1) * (n + 1) + 1`,如下左图所示,其中浅色代表空闲时间。 + +