All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0621_task_scheduler.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0601_0700.s0621_task_scheduler;

// #Medium #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Counting #Level_2_Day_5_Greedy
// #2022_03_21_Time_3_ms_(84.32%)_Space_59.6_MB_(58.24%)

public class Solution {
    public int leastInterval(char[] tasks, int n) {
        if (n <= 0) {
            return tasks.length;
        }
        int[] counters = new int[26];
        int maxCount = 0;
        for (char task : tasks) {
            int idx = task - 'A';
            counters[idx]++;
            maxCount = Math.max(maxCount, counters[idx]);
        }
        int maxNum = 0;
        for (int counter : counters) {
            if (counter == maxCount) {
                maxNum++;
            }
        }
        return Math.max(tasks.length, (maxCount - 1) * (n + 1) + maxNum);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy