返回介绍

solution / 0600-0699 / 0621.Task Scheduler / README_EN

发布于 2024-06-17 01:03:36 字数 5846 浏览 0 评论 0 收藏 0

621. Task Scheduler

中文文档

Description

You are given an array of CPU tasks, each represented by letters A to Z, and a cooling time, n. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: identical tasks must be separated by at least n intervals due to cooling time.

​Return the _minimum number of intervals_ required to complete all tasks.

 

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2

Output: 8

Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.

After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th cycle, you can do A again as 2 intervals have passed.

Example 2:

Input: tasks = ["A","C","A","B","D","B"], n = 1

Output: 6

Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.

With a cooling interval of 1, you can repeat a task after just one other task.

Example 3:

Input: tasks = ["A","A","A", "B","B","B"], n = 3

Output: 10

Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.

There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.

 

Constraints:

  • 1 <= tasks.length <= 104
  • tasks[i] is an uppercase English letter.
  • 0 <= n <= 100

Solutions

Solution 1

class Solution:
  def leastInterval(self, tasks: List[str], n: int) -> int:
    cnt = Counter(tasks)
    x = max(cnt.values())
    s = sum(v == x for v in cnt.values())
    return max(len(tasks), (x - 1) * (n + 1) + s)
class Solution {
  public int leastInterval(char[] tasks, int n) {
    int[] cnt = new int[26];
    int x = 0;
    for (char c : tasks) {
      c -= 'A';
      ++cnt[c];
      x = Math.max(x, cnt[c]);
    }
    int s = 0;
    for (int v : cnt) {
      if (v == x) {
        ++s;
      }
    }
    return Math.max(tasks.length, (x - 1) * (n + 1) + s);
  }
}
class Solution {
public:
  int leastInterval(vector<char>& tasks, int n) {
    vector<int> cnt(26);
    int x = 0;
    for (char c : tasks) {
      c -= 'A';
      ++cnt[c];
      x = max(x, cnt[c]);
    }
    int s = 0;
    for (int v : cnt) {
      s += v == x;
    }
    return max((int) tasks.size(), (x - 1) * (n + 1) + s);
  }
};
func leastInterval(tasks []byte, n int) int {
  cnt := make([]int, 26)
  x := 0
  for _, c := range tasks {
    c -= 'A'
    cnt[c]++
    x = max(x, cnt[c])
  }
  s := 0
  for _, v := range cnt {
    if v == x {
      s++
    }
  }
  return max(len(tasks), (x-1)*(n+1)+s)
}
public class Solution {
  public int LeastInterval(char[] tasks, int n) {
    int[] cnt = new int[26];
    int x = 0;
    foreach (char c in tasks) {
      cnt[c - 'A']++;
      x = Math.Max(x, cnt[c - 'A']);
    }
    int s = 0;
    foreach (int v in cnt) {
      s = v == x ? s + 1 : s;
    }
    return Math.Max(tasks.Length, (x - 1) * (n + 1) + s);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文