LeetCode621. Task Scheduler

Today, I did it after reading the half-day problem-solving report. Then let's analyze the problem solving report.

Click to open the link


topic

Given a list of tasks that the CPU needs to perform, represented by a character array. It contains 26 different kinds of tasks using uppercase A - Z letters. Tasks can be executed in any order, and each task can be executed in 1 unit of time. The CPU can execute a task in any unit of time, or be in a standby state.

However, there must be a cooldown period of length  n between two tasks of the same kind , so at least n consecutive units of time the CPU is executing a different task, or is in a standby state.

You need to calculate the minimum time required to complete all tasks .

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2
 Output: 8
 Execution order: A -> B -> (standby) -> A -> B -> (standby) -> A -> B.

Note:

  1. The total number of tasks is [1, 10000].
  2. The value range of n is [0, 100].

analyze

The general idea is to first count the frequency of occurrence of each letter, save it in the count array, and then sort it in ascending order from small to large, that is, the last frequency in the array is the frequency with the most occurrences, and the maximum frequency is also is count[25].

What we need to do is to insert the letter with the most frequency, such as A, in the middle, and insert (n-1) letters (including the standby command) between the two adjacent A, because the frequency of occurrence of A is count[ 25], that is, we need to insert count[25]-1 blanks at least, so the time to remove the most frequent letter A that does not need to be inserted at the end is: count[25]-1*(n+1), where the n+1 is n empty+A;

After the empty insertion, the letter A with the highest frequency will remain. Of course, the given data may have multiple letters with the same frequency, and there may also be BCD..., we need to add them all and sort them out. The final formula is: count[25]-1*(n+1) + 25-temp (25-temp is the number of letters with the highest frequency).

Um. . . We can also find that if it is just full, that is, when it is not on standby, the required time is the length of the array. At this time, if the above formula is used to calculate, there may be a time when the time is less than the array, so the larger of the two should be returned.


code

class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];

        for (int i = 0; i < tasks.length; i++) {
            count[tasks[i]-'A']++;
        }//Statistics word frequency

        Arrays.sort(count);//Word frequency sorting, ascending sorting, count[25] is the most frequent

        int temp = 25;//At most 25 letters and the same frequency as the highest frequency letter
        while ( temp >= 0 && count[25] == count[temp]){
            temp--;
        }

        return Math.max((count[25] - 1) * (n + 1) + 25 - temp , tasks.length);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325897526&siteId=291194637