621. Task Scheduler (Medium)

https://leetcode.com/problems/task-scheduler/

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.

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.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

 

Example:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

 

Note:

  1. The number of tasks is in the range [1, 10000].
  2. The integer n is in the range [0, 100].

Solutions

public class Solution {

    public int leastInterval(char[] tasks, int n) {
        if (tasks == null || tasks.length == 0) {
            return 0;
        }

        if (n == 0) {
            return tasks.length;
        }

        int[] count = new int[26];
        for(char t : tasks){
            count[t - 'A']++;
        }

        Arrays.sort(count);

        int i = 25;
        while(i >= 0 && count[i] == count[25]) {
            i--;
        }

        // tasks="AAAABBBBEEFFGG", n=3

        // Assume the time as chunks
        // initially, AXXX,AXXX,AXXX,A
        // insert B, ABXX,ABXX,ABXX,AB
        // ...

        // (count[25]-1) means chunks - 1
        // (n+1) means the chunk size
        // 25 - i means the last chunk you don't have to count in the idle time
        return Math.max(tasks.length, (count[25] - 1) * (n + 1) + 25 - i);
    }
}

2.

public class Solution {

    // This problem is hard to figure out.

    public int leastInterval(char[] tasks, int n) {
        if (n == 0) {
            return tasks.length;
        }

        Map<Character, Integer> taskFrequence = new HashMap<>();
        for (char c : tasks) {
            if (!taskFrequence.containsKey(c)) {
                taskFrequence.put(c, 0);
            }

            taskFrequence.put(c, taskFrequence.get(c) + 1);
        }

        Queue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
        for (char c : taskFrequence.keySet()) {
            queue.offer(taskFrequence.get(c));
        }

        int currTime = 0;

        Map<Integer, Integer> coolDown = new HashMap<>();

        while (!queue.isEmpty() || !coolDown.isEmpty()) {

            // get the remained execution times of certain task executed at currTime-n-1, it could be idle
            // at that time if not exists in coolDown map
            if (coolDown.containsKey(currTime - n - 1)) {

                // put the remained execution times to the queue
                queue.offer(coolDown.remove(currTime - n - 1));
            }

            if (!queue.isEmpty()) {
                // pick the task of most quantity to execute
                int remanent = queue.poll() - 1;

                if (remanent != 0) {
                    coolDown.put(currTime, remanent);
                }
            }

            currTime++;
        }

        return currTime;
    }
}

Incorrect Solutions

1.

class Solution {

    // Incorrect Solution

    public int leastInterval(char[] tasks, int n) {
        if (tasks.length == 0) {
            return 0;
        }

        if (n == 0) {
            return tasks.length;
        }

        int[] freq = new int[26];
        for (char t : tasks) {
            freq[t - 'A']++;
        }

        int[] occurrence = new int[26];
        Arrays.fill(occurrence, -1);

        int len = tasks.length;

        int charCount = 0;
        int totalCount = 0;

        int idx = 0;
        while (charCount < len) {
            int i = idx % 26;

            while (freq[i] == 0) {
                idx++;
                i = idx % 26;
            }

            idx++;
            freq[i]--;
            charCount++;
            totalCount++;

            if (occurrence[i] < 0) {
                occurrence[i] = totalCount - 1;

                continue;
            }

            int idle = n - (totalCount - 2 - occurrence[i]);

            totalCount += Math.max(0, idle);

            occurrence[i] = totalCount - 1;
        }

        return totalCount;
    }
}

2.

class Solution {

    // Incorrect Solution

    public int leastInterval(char[] tasks, int n) {
        int ans = 0;
        if (tasks.length == 0) {
            return ans;
        }

        if (n == 0) {
            return tasks.length;
        }

        int[] freq = new int[26];
        for (char t : tasks) {
            freq[t - 'A']++;
        }

        int remain = tasks.length;

        while (remain > 0) {
            int count = 0;
            for (int i = 0; i < 26; i++) {
                if (freq[i] == 0) {
                    continue;
                }

                freq[i]--;
                count++;

                if (count > n) {
                    break;
                }
            }

            remain -= count;
            if (remain == 0) {
                ans += count;
                break;
            }

            int idle = 0;
            if (count < n + 1) {
                idle = n + 1 - count;
            }

            ans += idle + count;
        }

        return ans;
    }
}

References

  1. https://leetcode.com/problems/task-scheduler/discuss/104496/concise-Java-Solution-O(N)-time-O(26)-space
  2. https://leetcode.com/problems/task-scheduler/discuss/104511/Java-Solution-PriorityQueue-and-HashMap
Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-12-03 11:01:17

results matching ""

    No results matching ""