1
00:00:00,720 --> 00:00:01,770
Welcome back.

2
00:00:01,770 --> 00:00:06,690
Let's now get into the code for solving the task scheduler question okay.

3
00:00:06,690 --> 00:00:09,090
So we are given this array over here tasks.

4
00:00:09,090 --> 00:00:13,410
And it's given that every task is a letter from A to Z.

5
00:00:13,530 --> 00:00:15,210
The capital letters okay.

6
00:00:15,210 --> 00:00:19,860
So we will need an array to hold the frequency of each task.

7
00:00:19,860 --> 00:00:22,320
And let me just call it counter okay.

8
00:00:22,320 --> 00:00:24,630
So counter is equal to new array.

9
00:00:24,630 --> 00:00:27,480
And this is going to have 26 elements.

10
00:00:27,480 --> 00:00:33,300
And initially we will just fill every element in this array with the value zero okay.

11
00:00:33,300 --> 00:00:35,010
And let's have another variable.

12
00:00:35,010 --> 00:00:36,600
Let's just call it max val.

13
00:00:36,600 --> 00:00:42,900
And this is going to be the maximum frequency of any task in this tasks array over here.

14
00:00:42,900 --> 00:00:46,500
And initially we will say that max val is equal to zero.

15
00:00:46,500 --> 00:00:49,110
And we will also need another variable.

16
00:00:49,110 --> 00:00:54,210
Let me just call it number tasks highest frequency okay.

17
00:00:54,210 --> 00:01:00,720
So this is going to hold the number of tasks that are there which have the highest frequency.

18
00:01:00,720 --> 00:01:03,540
And initially let me just set this also to zero.

19
00:01:03,540 --> 00:01:04,230
All right.

20
00:01:04,230 --> 00:01:11,100
Now what we will do is we will go through each task in the tasks array, and we will go ahead and store

21
00:01:11,100 --> 00:01:15,630
the frequency of that particular task in its respective position in this array.

22
00:01:15,660 --> 00:01:16,020
Okay.

23
00:01:16,020 --> 00:01:22,560
So for example for the task b we would increment the index one inch the counter array.

24
00:01:22,560 --> 00:01:28,470
Because over here we would have the indices zero, one, two, three, etc. going up to 25.

25
00:01:28,470 --> 00:01:35,490
And at index zero we will store the frequency of task A, and at index one we will store the frequency

26
00:01:35,490 --> 00:01:38,280
of task B and it goes on in this manner okay.

27
00:01:38,280 --> 00:01:39,990
So let's go ahead and implement that.

28
00:01:39,990 --> 00:01:43,380
So we can say tasks dot for each.

29
00:01:43,740 --> 00:01:48,960
And we are going to go through each task in the tasks array.

30
00:01:49,170 --> 00:01:53,100
And first we will have to identify the respective index.

31
00:01:53,100 --> 00:01:55,590
Like for example if it's B it's one right.

32
00:01:55,590 --> 00:01:56,970
So we'll have to find that.

33
00:01:56,970 --> 00:01:59,190
So for that let me use a variable.

34
00:01:59,190 --> 00:02:01,950
Let's just call it index task okay.

35
00:02:01,950 --> 00:02:11,340
And this is going to equal task dot char code at zero which again remember the char code at function

36
00:02:11,340 --> 00:02:17,820
in JavaScript is used to return the Unicode character code of a character at a specific index within

37
00:02:17,820 --> 00:02:18,240
a string.

38
00:02:18,240 --> 00:02:18,570
Okay.

39
00:02:18,570 --> 00:02:22,440
So task over here would be a string with one letter okay.

40
00:02:22,440 --> 00:02:28,080
It could be A, B, C, etc. and this over here will give us the Unicode character code of this particular

41
00:02:28,080 --> 00:02:28,980
task okay.

42
00:02:28,980 --> 00:02:35,970
And then we would do minus a dot char code at zero okay.

43
00:02:35,970 --> 00:02:37,470
So what's happening over here.

44
00:02:37,470 --> 00:02:40,470
Like let's say the task at hand is the task C.

45
00:02:40,590 --> 00:02:44,850
And for C this over here would give us 67.

46
00:02:44,850 --> 00:02:47,970
And this over here gives us 65.

47
00:02:47,970 --> 00:02:55,440
Because the Unicode character code of capital A is 65 and 67 -65 is equal to two.

48
00:02:55,440 --> 00:03:01,590
And notice that we actually want to store the frequency of task C at index two okay.

49
00:03:01,590 --> 00:03:02,970
So that's what we're doing over here.

50
00:03:02,970 --> 00:03:09,390
Now once we get the index we're going to increment the value at that particular index in the counter

51
00:03:09,390 --> 00:03:09,690
array.

52
00:03:09,690 --> 00:03:13,530
So I can say counter index task plus plus okay.

53
00:03:13,530 --> 00:03:21,840
And then we are going to check whether max val is equal to counter at index task.

54
00:03:22,380 --> 00:03:29,520
Because if that is the case then we would have identified one more task which has the highest frequency.

55
00:03:29,520 --> 00:03:34,620
And therefore we would have to increment number tasks highest frequency okay.

56
00:03:34,620 --> 00:03:37,080
And we will also need to check over here.

57
00:03:37,080 --> 00:03:44,400
If this is not the case then we would have to check whether max val is less than counter at index task,

58
00:03:44,400 --> 00:03:52,050
because if that is the case, then we have identified a task which has a higher frequency than the previously

59
00:03:52,050 --> 00:03:58,530
thought highest frequency, and therefore we would have to update max val to equal counter at index

60
00:03:58,530 --> 00:03:59,040
task.

61
00:03:59,040 --> 00:04:01,500
And we will also have to set number tasks.

62
00:04:01,500 --> 00:04:04,200
Highest frequency to be equal to one.

63
00:04:04,200 --> 00:04:05,970
And again why is this the case.

64
00:04:05,970 --> 00:04:09,420
Let's say initially we thought that the highest frequency is three.

65
00:04:09,420 --> 00:04:13,350
And let's say we identified two such tasks okay.

66
00:04:13,350 --> 00:04:19,200
Now if we find that the highest frequency is equal to four, then there are no longer two such tasks,

67
00:04:19,200 --> 00:04:20,370
but rather just one.

68
00:04:20,370 --> 00:04:21,930
So that's what we're doing over here.

69
00:04:21,930 --> 00:04:28,470
Okay, now once we are out of this loop, we will go ahead and try to find the number of idols as we

70
00:04:28,470 --> 00:04:34,080
have discussed in the previous video, once we get this, we can just return tasks dot length.

71
00:04:34,940 --> 00:04:36,770
Plus the number of idols.

72
00:04:36,770 --> 00:04:38,180
So that's our aim.

73
00:04:38,210 --> 00:04:38,870
Okay.

74
00:04:38,870 --> 00:04:44,330
And to get the number of idols, we will first have to find the number of parts.

75
00:04:44,660 --> 00:04:45,140
Okay.

76
00:04:45,140 --> 00:04:50,000
And then we will also need to identify the slots per part.

77
00:04:51,080 --> 00:04:51,500
Okay.

78
00:04:51,500 --> 00:04:54,140
And again I'm just having placeholders over here for now.

79
00:04:54,140 --> 00:04:57,140
And then we'll have to identify the empty slots.

80
00:04:57,500 --> 00:04:57,770
Okay.

81
00:04:57,770 --> 00:04:59,360
The total number of empty slots.

82
00:04:59,360 --> 00:05:02,540
And finally we'll also need the available tasks.

83
00:05:03,320 --> 00:05:05,750
So let's have placeholders for all of these.

84
00:05:05,750 --> 00:05:08,930
And again we have discussed this in detail in the previous video.

85
00:05:08,930 --> 00:05:15,800
So the number of parts is just going to be equal to max val minus one okay.

86
00:05:15,800 --> 00:05:23,720
And the slots per part will be equal to n minus the number of tasks with the highest frequency minus

87
00:05:23,720 --> 00:05:24,110
one.

88
00:05:24,350 --> 00:05:27,200
Again we have discussed this in detail in the previous video.

89
00:05:27,200 --> 00:05:32,510
And once we have these two we just need to multiply these two to get the empty slots.

90
00:05:32,510 --> 00:05:37,610
So I can say empty slots is equal to parts into slots per part.

91
00:05:38,030 --> 00:05:38,510
Okay.

92
00:05:38,510 --> 00:05:49,040
And the available tasks is the length of the tasks array minus max val into the number of tasks with

93
00:05:49,040 --> 00:05:50,540
the highest frequency.

94
00:05:50,540 --> 00:05:50,990
Okay.

95
00:05:50,990 --> 00:05:57,560
And once you get this to find the number of idols, typically you would just need to do empty slots

96
00:05:57,560 --> 00:05:59,990
minus the available tasks.

97
00:05:59,990 --> 00:06:04,610
And again, as we have discussed in the previous video, this alone is not sufficient.

98
00:06:04,610 --> 00:06:12,140
We will have to do math dot max between zero and this value over here, because we don't want idols

99
00:06:12,140 --> 00:06:14,450
at any point to be a negative value.

100
00:06:14,450 --> 00:06:19,250
Like for example, if the value n is equal to one.

101
00:06:19,890 --> 00:06:23,910
And let's say the tasks array is a, a, b b, c, d.

102
00:06:23,970 --> 00:06:24,420
Okay.

103
00:06:24,420 --> 00:06:29,220
So if you are going to fill the tasks as per the logic that we have discussed, we would initially have

104
00:06:29,220 --> 00:06:31,350
a, then we would have an empty space.

105
00:06:31,350 --> 00:06:35,910
Again we have a and we would fill B over here and B over here.

106
00:06:35,910 --> 00:06:42,690
And notice at this point the number of empty slots is equal to zero, and therefore empty slots minus

107
00:06:42,690 --> 00:06:45,030
available tasks would give us a negative value.

108
00:06:45,030 --> 00:06:50,190
But we don't want a negative value because in this case notice we would just fill C and D over here.

109
00:06:50,190 --> 00:06:54,210
And in this case the task length itself is sufficient.

110
00:06:54,210 --> 00:06:57,780
So idols should either be zero or a positive value.

111
00:06:57,780 --> 00:07:04,560
So that's why we are saying idols is equal to math dot max between what we get over here and zero and

112
00:07:04,560 --> 00:07:05,010
that's it.

113
00:07:05,010 --> 00:07:07,050
So this should help us solve this question.

114
00:07:07,050 --> 00:07:11,160
Now let's go ahead and run this code and see if it's passing all the test cases.

115
00:07:12,440 --> 00:07:14,210
And you can see that it's working.

116
00:07:14,210 --> 00:07:15,920
It's passing all the test cases.

117
00:07:15,920 --> 00:07:20,960
So this is the greedy approach that we can take to solve the task scheduler question.
