DEV Community

Wenqi Jiang
Wenqi Jiang

Posted on

621. Task Scheduler

Description

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.

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.

Return the least number of units of times that the CPU will take to finish all the given tasks.

💡 Note:

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: 
A -> B -> idle -> A -> B -> idle -> A -> B
There is at least 2 units of time between any two same tasks.
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: tasks = ["A","A","A","B","B","B"], n = 0
Output: 6
Explanation: On this case any permutation of size 6 would work since n = 0.
["A","A","A","B","B","B"]
["A","B","A","B","A","B"]
["B","B","B","A","A","A"]
...
And so on.
Enter fullscreen mode Exit fullscreen mode

Example 3:

Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output: 16
Explanation: 
One possible solution is
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
Enter fullscreen mode Exit fullscreen mode

Constraints:

  • 1 <= task.length <= 104
  • tasks[i] is upper-case English letter.
  • The integer n is in the range [0, 100].

Solutions

Solution 1

Intuition

Code

public int leastInterval(char[] tasks, int n) {
    int[] frequencies = new int[26];
    int max = Integer.MIN_VALUE;
    for (int task : tasks) {
        frequencies[task - 'A']++;
        max = Math.max(max, frequencies[task - 'A']);
    }
    int maxCount = 0;
    for (int frequency : frequencies) {
        if (frequency == max) {
            maxCount++;
        }
    }
    return Math.max(tasks.length, (max - 1) * (n + 1) + maxCount);
}
Enter fullscreen mode Exit fullscreen mode

Complexity

  • Time: O(n)
  • Space: O(1)

Solution 2

Intuition

  1. create the task frequencies;
  2. you need build a heap from big to small
  3. process each task in slots
  4. remember the last slot, you need to check if heap is empty, and add working time rather than the whole slot

Code

public int leastInterval(char[] tasks, int n) {
    HashMap<Character, Integer> frequencies = new HashMap<>();
    for (char task : tasks) {
        frequencies.put(task, frequencies.getOrDefault(task, 0) + 1);
    }
    PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
    heap.addAll(frequencies.values());

    int slot = n + 1, time = 0;
    while (!heap.isEmpty()) {
        int workingTime = 0;
        ArrayList<Integer> unfinishedTasks = new ArrayList<>();
        for (int i = 0; i < slot; i++) {
            if (!heap.isEmpty()) {
                                // process task: task - 1
                unfinishedTasks.add(heap.poll() - 1);
                workingTime++;
            }
        }
        for (Integer unfinishedTask : unfinishedTasks) {
            if (unfinishedTask > 0) {
                heap.add(unfinishedTask);
            }
        }
        time += heap.isEmpty() ? workingTime : slot;
    }
    return time;
}
Enter fullscreen mode Exit fullscreen mode

Complexity

  • Time: O(n)
  • Space: O(1)

Top comments (0)