[LeetCode] 621. Task Scheduler

来源:互联网 发布:iis ip地址和域名限制 编辑:程序博客网 时间:2024/05/17 06:37

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 1:

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

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

class Solution {public:    int leastInterval(vector<char>& tasks, int n) {        vector<int> count(26, 0);        for (auto tsk : tasks) count[tsk - 'A']++;        int MaxSingleNTask = 0;        for (auto cnt : count)            MaxSingleNTask = max(MaxSingleNTask, cnt);        int LeastCost = (MaxSingleNTask - 1) * (n + 1);        for (auto cnt : count)            LeastCost += (cnt == MaxSingleNTask);        return max((int)tasks.size(), LeastCost);    }};

这里写图片描述这里写图片描述

原创粉丝点击