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
  • 相关阅读:
    pyftpdlib 搭建FTP服务器
    numpy 解一道简单数学题
    python 实现词云
    个人的毕业长足---- 暴走北京
    Tensorflow of GPU, hello fish by version 0.8.
    图像识别
    用一个Inception v3 架构模型实现简单的迁移学习(译:../tensorflow/tensorflow/examples/image_retraining/retrain.py)
    19.液晶屏的原理
    18.DMA-6410
    17.DMA-2440
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9006990.html
Copyright © 2011-2022 走看看