LeetCode 第621题:任务调度器

2023-03-11  本文已影响0人  放开那个BUG

1、前言

题目描述

2、思路

先找到次数最多的任务排好,其他任务插在中间,类似于这样:


解题

3、代码

class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] cnts = new int[26];
        for(char c : tasks){
            cnts[c - 'A']++;
        }
        int max = 0, tot = 0;
        for(int i = 0; i < 26; i++){
            max = Math.max(max, cnts[i]);
        }
        for(int i = 0; i < 26; i++){
            tot += max == cnts[i] ? 1 : 0;
        }

        return Math.max(tasks.length, (n + 1) * (max - 1) + tot);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读