LeetCode 621 Task Scheduler (贪心 思维 推荐)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tc_To_Top/article/details/88808321

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].

题目链接:https://leetcode.com/problems/task-scheduler/

题目分析:先暴力模拟一发贪心:从频数大的开始排,每轮安排任务均排到n+1个为止

26ms,时间击败43%

class Solution {
    
    class Data {
        int idx, num;
        Data(int idx, int num) {
            this.idx = idx;
            this.num = num;
        }
    }
    
    public int leastInterval(char[] tasks, int n) {
        Data[] data = new Data[26];
        int[] nums = new int[26];
        for (char ch : tasks) {
            nums[ch - 'A']++;
        }
        for (int i = 0; i < 26; i++) {
            data[i] = new Data(i, nums[i]);   
        }
        Comparator cmp = new Comparator<Data>() {
            @Override
            public int compare(Data a, Data b) {
                return b.num - a.num;
            }
        };
        int curPos = 0, cnt = 0;
        int[] pos = new int[10005];
        while (cnt < tasks.length) {
            Arrays.sort(data, cmp);
            for (int i = 0; i <= Math.min(26, n) && data[i].num > 0; i++) {
                int ch = data[i].idx;
                if (pos[ch] == 0 || curPos - pos[ch] > n) {
                    curPos += 1;
                } else {
                    curPos += n - (curPos - pos[ch]) + 1;
                }
                cnt++;
                pos[ch] = curPos;
                data[i].num--;
            }
        }
        return curPos;
    }
}

从上面的暴力可以发现频数最大的数字必然会贯穿整个任务序列,不妨将所有频数最大的字符取出来组合为X(代表一个任务集),构造序列:X(t)X(t)...X,其中(t)表示任意t个不包含X中任务的任务,设X的长度为XLen,X出现的次数为XCnt

根据题意有XLen - 1 + |t| = n => |t| = n - XLen + 1,将X(t)看做整体,则|X(t)| = |X| + |t| = XLen + n - XLen + 1 = n + 1,构造的序列中存在XCnt - 1个X(t)及一个单个的X,因此|X(t)X(t)...X| = (XCnt - 1) * (n + 1) + XLen,该值算出来可能比原任务总数小,因为我们在计算时是默认间隔等于n的,有可能实际情况间隔会超过n,超过的部分直接补在中间即可(即将(t)增大)

举个例子AABBCCCDDD,n=2,故最终答案应与任务总数取最大

4ms,时间击败100%

class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] frqs = new int[26];
        for (char ch : tasks) {
            frqs[ch - 'A']++;
        }
        int XCnt = 0, XLen = 0;
        for (int frq : frqs) {
            if (frq > XCnt) {
                XCnt = frq;
                XLen = 1;
            } else if (frq == XCnt) {
                XLen++;
            }
        }
        return Math.max(tasks.length, (XCnt - 1) * (n + 1) + XLen);
    }
}

猜你喜欢

转载自blog.csdn.net/Tc_To_Top/article/details/88808321