frequencies
of size 26 to zero, to store the frequency of each task (since there are at most 26 tasks represented by uppercase English letters).tasks
array and increment the corresponding frequency in the frequencies
array.frequencies
array in descending order.(frequencies[0] - 1) * n
, where frequencies[0]
is the frequency of the most common task.frequencies
array starting from the second element. For each frequency f
, decrease the idle time by min(frequencies[0] - 1, f)
.tasks
array and the calculated idle time.