• 621. Task Scheduler


    问题:

    给定任务数组,每个任务用一个大写字母表示。

    给定相同任务之间的缓冲间隔 n(间隔中可填充其他任务,若没有则使之空闲 idle )

    求对给定任务一览进行安排,使得所用时间最少,求此时间。

    Example:
    Input: tasks = ["A","A","A","B","B","B"], n = 2
    Output: 8
    Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
     
    Constraints:
    The number of tasks is in the range [1, 10000].
    The integer n is in the range [0, 100].
    

      

    解法:

    使用unordered_map来存放每个任务及其数量,求得最多的任务数maxcout,这里假设为 A

    则有figure2和3两种情况。

    figure2: 所有任务未占满,那么此时n+1=5,maxcout-1=3,再加上相同任务数量为maxcout的任务数(A,B)=3*5+2=17

    figure3: 所有任务占满(没有空闲时间idle slot),那么直接等于nums.size()=19

    参考代码:

     1 class Solution {
     2 public:
     3     int leastInterval(vector<char>& tasks, int n) {
     4         unordered_map<char,int> tasksm;
     5         unordered_map<char,int>::iterator it;
     6         int maxcout=0;
     7         for(char i:tasks){
     8             tasksm[i]++;
     9             maxcout=max(maxcout, tasksm[i]);
    10         }
    11         int res=0;
    12         res = (n+1)*(maxcout-1);
    13         for(auto e:tasksm){
    14             if(e.second==maxcout) res++;
    15         }
    16         res = max(res, (int)tasks.size());
    17         return res;
    18     }
    19 };
  • 相关阅读:
    Ajax调用处理页面错误信息500的解决思路
    PHP数据库的增删改
    PHP登录及处理
    PHP数据访问
    PHP数组(正则表达式、数组、预定义数组)
    php函数
    45
    数据库_CRUD操作之读取数据之高级读取
    数据库_CRUD操作之读取数据
    数据库_CRUD操作之修改数据
  • 原文地址:https://www.cnblogs.com/habibah-chang/p/12632637.html
Copyright © 2020-2023  润新知