Welcome back.
Let's now get into the code for solving the task scheduler question okay.
So we are given this array over here tasks.
And it's given that every task is a letter from A to Z.
The capital letters okay.
So we will need an array to hold the frequency of each task.
And let me just call it counter okay.
So counter is equal to new array.
And this is going to have 26 elements.
And initially we will just fill every element in this array with the value zero okay.
And let's have another variable.
Let's just call it max val.
And this is going to be the maximum frequency of any task in this tasks array over here.
And initially we will say that max val is equal to zero.
And we will also need another variable.
Let me just call it number tasks highest frequency okay.
So this is going to hold the number of tasks that are there which have the highest frequency.
And initially let me just set this also to zero.
All right.
Now what we will do is we will go through each task in the tasks array, and we will go ahead and store
the frequency of that particular task in its respective position in this array.
Okay.
So for example for the task b we would increment the index one inch the counter array.
Because over here we would have the indices zero, one, two, three, etc. going up to 25.
And at index zero we will store the frequency of task A, and at index one we will store the frequency
of task B and it goes on in this manner okay.
So let's go ahead and implement that.
So we can say tasks dot for each.
And we are going to go through each task in the tasks array.
And first we will have to identify the respective index.
Like for example if it's B it's one right.
So we'll have to find that.
So for that let me use a variable.
Let's just call it index task okay.
And this is going to equal task dot char code at zero which again remember the char code at function
in JavaScript is used to return the Unicode character code of a character at a specific index within
a string.
Okay.
So task over here would be a string with one letter okay.
It could be A, B, C, etc. and this over here will give us the Unicode character code of this particular
task okay.
And then we would do minus a dot char code at zero okay.
So what's happening over here.
Like let's say the task at hand is the task C.
And for C this over here would give us 67.
And this over here gives us 65.
Because the Unicode character code of capital A is 65 and 67 -65 is equal to two.
And notice that we actually want to store the frequency of task C at index two okay.
So that's what we're doing over here.
Now once we get the index we're going to increment the value at that particular index in the counter
array.
So I can say counter index task plus plus okay.
And then we are going to check whether max val is equal to counter at index task.
Because if that is the case then we would have identified one more task which has the highest frequency.
And therefore we would have to increment number tasks highest frequency okay.
And we will also need to check over here.
If this is not the case then we would have to check whether max val is less than counter at index task,
because if that is the case, then we have identified a task which has a higher frequency than the previously
thought highest frequency, and therefore we would have to update max val to equal counter at index
task.
And we will also have to set number tasks.
Highest frequency to be equal to one.
And again why is this the case.
Let's say initially we thought that the highest frequency is three.
And let's say we identified two such tasks okay.
Now if we find that the highest frequency is equal to four, then there are no longer two such tasks,
but rather just one.
So that's what we're doing over here.
Okay, now once we are out of this loop, we will go ahead and try to find the number of idols as we
have discussed in the previous video, once we get this, we can just return tasks dot length.
Plus the number of idols.
So that's our aim.
Okay.
And to get the number of idols, we will first have to find the number of parts.
Okay.
And then we will also need to identify the slots per part.
Okay.
And again I'm just having placeholders over here for now.
And then we'll have to identify the empty slots.
Okay.
The total number of empty slots.
And finally we'll also need the available tasks.
So let's have placeholders for all of these.
And again we have discussed this in detail in the previous video.
So the number of parts is just going to be equal to max val minus one okay.
And the slots per part will be equal to n minus the number of tasks with the highest frequency minus
one.
Again we have discussed this in detail in the previous video.
And once we have these two we just need to multiply these two to get the empty slots.
So I can say empty slots is equal to parts into slots per part.
Okay.
And the available tasks is the length of the tasks array minus max val into the number of tasks with
the highest frequency.
Okay.
And once you get this to find the number of idols, typically you would just need to do empty slots
minus the available tasks.
And again, as we have discussed in the previous video, this alone is not sufficient.
We will have to do math dot max between zero and this value over here, because we don't want idols
at any point to be a negative value.
Like for example, if the value n is equal to one.
And let's say the tasks array is a, a, b b, c, d.
Okay.
So if you are going to fill the tasks as per the logic that we have discussed, we would initially have
a, then we would have an empty space.
Again we have a and we would fill B over here and B over here.
And notice at this point the number of empty slots is equal to zero, and therefore empty slots minus
available tasks would give us a negative value.
But we don't want a negative value because in this case notice we would just fill C and D over here.
And in this case the task length itself is sufficient.
So idols should either be zero or a positive value.
So that's why we are saying idols is equal to math dot max between what we get over here and zero and
that's it.
So this should help us solve this question.
Now let's go ahead and run this code and see if it's passing all the test cases.
And you can see that it's working.
It's passing all the test cases.
So this is the greedy approach that we can take to solve the task scheduler question.