zoukankan      html  css  js  c++  java
  • 621. Task Scheduler CPU任务间隔分配器

    [抄题]:

    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 = 2
    Output: 8
    Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    知道不同字母之间互相制约,但是不知道抓主要矛盾:把字母频次由高到低统计

    [一句话思路]:

    字母种类少时套公式: 间隔数*(间隔长度+1自身)+ 补齐自身

     (max - 1) * (n + 1) + maxNum 

    ,种类多时直接用数组长

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    多个字母都会影响时,选择频率最高的主要影响

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [算法思想:递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public int leastInterval(char[] tasks, int n) {
            //cc
            
            
            //ini: char[26]
            int[] letters = new int[26];
            int i = 25;
            
            //store in char[26], sort, count frequency
            for (char c : tasks) {
                letters[c - 'A']++;
            }
            Arrays.sort(letters);
            while (i >= 0 && letters[i] == letters[25]) i--; 
            
            //return res
            return Math.max(tasks.length, (n + 1) * (letters[25] - 1) + 25 - i);
        }
    }
    View Code
  • 相关阅读:
    PHP7函数大全(4553个函数)
    Mysql 查看连接数,状态 最大并发数
    linux安装git
    PHP new StdClass() 创建空对象
    PHP 如何向关联数组指定的 Key 之前插入元素
    php 常用 小知识点
    PHP激活用户注册验证邮箱
    php rsa 加密、解密、签名、验签
    PHP支付接口RSA验证
    [2018-12-07]用ABP入门DDD
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9006990.html
Copyright © 2011-2022 走看看