1
00:00:00,780 --> 00:00:02,790
Hi, everyone, welcome to this new video.

2
00:00:03,090 --> 00:00:08,160
So in this video, we are going to solve one important question, which is rotting oranges.

3
00:00:09,000 --> 00:00:11,070
So in this question, we are provided with a grid.

4
00:00:11,070 --> 00:00:12,720
So we have one grid.

5
00:00:14,030 --> 00:00:18,870
And this grid is basically of size and McCrossin fine.

6
00:00:19,190 --> 00:00:24,320
Now, this grid can contain three values, zero one or two, so zero means empty cell.

7
00:00:24,350 --> 00:00:27,890
So this is not very useful to us in this problem.

8
00:00:28,100 --> 00:00:30,340
Only these two values are useful one and two.

9
00:00:30,560 --> 00:00:34,670
So one means fresh orange to means rotten orange.

10
00:00:35,060 --> 00:00:35,450
Right.

11
00:00:35,660 --> 00:00:37,580
So we have zero ones and twos.

12
00:00:37,580 --> 00:00:38,980
Zeros are not important.

13
00:00:39,060 --> 00:00:40,570
Let's say this is true.

14
00:00:40,580 --> 00:00:41,360
This is when.

15
00:00:41,360 --> 00:00:42,230
When when.

16
00:00:42,560 --> 00:00:43,160
This is when.

17
00:00:43,160 --> 00:00:45,680
This is when they are similarly.

18
00:00:45,680 --> 00:00:46,610
I have to here.

19
00:00:46,790 --> 00:00:52,970
I have one here, one here similar to I have two here, I have one here, one here and so on.

20
00:00:53,030 --> 00:00:53,390
Right.

21
00:00:53,780 --> 00:00:55,610
So Value Zero is not important to us.

22
00:00:55,610 --> 00:00:56,600
So we can ignore that.

23
00:00:57,470 --> 00:00:59,170
So every minute what will happen.

24
00:01:00,170 --> 00:01:07,550
So after one minute what will happen, the rotten orange will rot all the fresh orange which are horizontally

25
00:01:07,550 --> 00:01:09,330
connected or vertically connected.

26
00:01:10,700 --> 00:01:14,110
So let's talk about what will happen after one minute after one minute.

27
00:01:14,120 --> 00:01:16,900
This is your rotten orange, so it will rot.

28
00:01:17,030 --> 00:01:18,440
This orange and this orange.

29
00:01:18,650 --> 00:01:20,760
So rotten means change the value.

30
00:01:20,780 --> 00:01:22,220
So this will become two and two.

31
00:01:23,270 --> 00:01:27,500
Similarly, in that one minute only, this will also get rotten.

32
00:01:27,500 --> 00:01:29,520
These fresh oranges will also get rotten.

33
00:01:29,870 --> 00:01:35,690
Similarly, this orange will get rotten, this orange will get rotten, and this orange will get rotten.

34
00:01:35,960 --> 00:01:40,040
We can rot horizontally or we can vertically straight.

35
00:01:40,250 --> 00:01:42,650
So after one minute, this will be the scenario.

36
00:01:43,520 --> 00:01:44,500
This is after one.

37
00:01:45,870 --> 00:01:54,840
Now, let's talk about one minute after this, so one minute after this, what will happen so this rotten

38
00:01:54,840 --> 00:01:58,570
orange will rot, this fresh orange and this fresh orange.

39
00:01:59,580 --> 00:02:02,970
So this is the scenario after two minutes, right.

40
00:02:03,120 --> 00:02:04,170
And let it rest.

41
00:02:04,170 --> 00:02:05,310
All the values are zero.

42
00:02:06,990 --> 00:02:08,570
Arrest all the values at zero.

43
00:02:09,780 --> 00:02:16,530
So we need to return the number of minutes I will take to rot all the oranges.

44
00:02:16,530 --> 00:02:19,970
So in this case, how many time I took, I took two minutes.

45
00:02:19,980 --> 00:02:21,360
So too will be the answer.

46
00:02:22,650 --> 00:02:22,880
Right.

47
00:02:23,310 --> 00:02:25,800
So let the example which is provided to us.

48
00:02:26,640 --> 00:02:32,580
So this is the example, let's see, so in this example, we can see.

49
00:02:34,170 --> 00:02:35,710
This is let me right here.

50
00:02:35,730 --> 00:02:42,080
This is two one one, then one one zero, then zero one and one late.

51
00:02:42,240 --> 00:02:45,780
So let's talk about what will happen after one minute or so.

52
00:02:45,780 --> 00:02:46,980
After one minute.

53
00:02:48,110 --> 00:02:55,060
This is a road rage, so it will rot this orange, it will rot this orange right after two minutes,

54
00:02:55,070 --> 00:02:55,690
what will happen?

55
00:02:55,700 --> 00:03:00,050
So this too will rot this orange and it will rot this orange.

56
00:03:00,890 --> 00:03:04,010
So after three minutes, what will happen?

57
00:03:04,250 --> 00:03:06,620
So this two will rot this orange.

58
00:03:06,890 --> 00:03:07,300
Right.

59
00:03:07,580 --> 00:03:09,740
So after four minutes, what will happen?

60
00:03:10,040 --> 00:03:12,050
These two will rot this orange.

61
00:03:12,410 --> 00:03:14,720
Now we do not have any fresh orange.

62
00:03:15,110 --> 00:03:18,260
So how much time I took to not all the oranges for ten minutes.

63
00:03:18,440 --> 00:03:20,180
So that's why the output is four.

64
00:03:21,230 --> 00:03:21,620
Great.

65
00:03:21,920 --> 00:03:26,870
So let's take one more example, which is give an indication.

66
00:03:29,950 --> 00:03:36,780
Let's talk about this one, right, so I have two one one zero one one one zero one.

67
00:03:37,240 --> 00:03:39,030
So what will happen after one minute?

68
00:03:39,700 --> 00:03:41,580
So what will happen after one minute?

69
00:03:41,680 --> 00:03:45,520
This two will brought all the oranges which are horizontally or vertically connected.

70
00:03:45,820 --> 00:03:49,720
So it will become too that after two minutes, what will happen?

71
00:03:49,720 --> 00:03:52,330
This will rot, this orange and this orange.

72
00:03:52,810 --> 00:03:53,250
Correct.

73
00:03:53,830 --> 00:03:55,390
After three minutes, what will happen?

74
00:03:55,390 --> 00:03:56,580
These two will rot.

75
00:03:56,590 --> 00:03:57,340
This orange.

76
00:03:57,760 --> 00:03:58,220
Correct.

77
00:03:58,450 --> 00:04:00,100
So after four minutes, it will happen.

78
00:04:00,100 --> 00:04:01,030
This will rot.

79
00:04:01,030 --> 00:04:01,690
This orange.

80
00:04:01,930 --> 00:04:02,380
Correct.

81
00:04:03,100 --> 00:04:04,420
And that's it.

82
00:04:05,050 --> 00:04:05,320
Right.

83
00:04:05,440 --> 00:04:05,630
That's.

84
00:04:06,100 --> 00:04:10,720
So in this we have one fresh orange.

85
00:04:10,720 --> 00:04:14,130
That means I'm not able to write all the oranges.

86
00:04:15,460 --> 00:04:17,649
That's why the output will be minus one.

87
00:04:18,640 --> 00:04:19,089
Right.

88
00:04:19,209 --> 00:04:22,330
I'm not able to write all the fresh oranges.

89
00:04:22,330 --> 00:04:25,690
Fresh oranges are still present, so the output is minus one.

90
00:04:26,620 --> 00:04:28,000
Now, let's talk about this.

91
00:04:28,010 --> 00:04:29,500
So this is very simple.

92
00:04:29,500 --> 00:04:31,180
012, right?

93
00:04:31,480 --> 00:04:34,390
So you can see there are no thresholding.

94
00:04:35,460 --> 00:04:37,110
There are no fresh oranges.

95
00:04:37,130 --> 00:04:40,560
So how much time I will take to get all the fresh oranges zero.

96
00:04:41,700 --> 00:04:44,790
Ditto because fresh oranges are not there, right?

97
00:04:46,080 --> 00:04:59,340
Now, let's discuss one last example, let's say I have this two, one one and I have like two one two

98
00:04:59,340 --> 00:05:05,460
zero and I have zero one and one said this is the example.

99
00:05:05,790 --> 00:05:06,180
Right.

100
00:05:06,570 --> 00:05:09,960
So how much time I will take to write all the fresh oranges.

101
00:05:10,410 --> 00:05:14,480
So let's talk about after one minutes after one minute what will happen.

102
00:05:14,490 --> 00:05:19,050
These two will write this fresh orange and this fresh orange.

103
00:05:19,620 --> 00:05:24,000
Similarly, in that one minute only these two will also write.

104
00:05:24,300 --> 00:05:25,890
So it will read this one.

105
00:05:26,250 --> 00:05:26,610
Right.

106
00:05:26,770 --> 00:05:28,880
So this is the scenario after one minute.

107
00:05:29,730 --> 00:05:37,050
So what I want to say is all the rotten oranges will rot, all fresh oranges.

108
00:05:37,260 --> 00:05:37,650
Great.

109
00:05:37,740 --> 00:05:40,020
And we can have multiple rotten oranges.

110
00:05:41,160 --> 00:05:43,680
We can have multiple rotten oranges.

111
00:05:45,190 --> 00:05:53,100
So in one minute, all rotten oranges will rot there horizontally, vertically connected, fresh oranges,

112
00:05:53,110 --> 00:05:55,300
so in one minute this will be the scenario.

113
00:05:55,960 --> 00:05:59,620
And after one more minute, after two minutes, what will happen?

114
00:05:59,950 --> 00:06:01,350
These two will rot.

115
00:06:01,360 --> 00:06:03,540
This one these two will rot.

116
00:06:03,550 --> 00:06:05,020
This one, right.

117
00:06:05,050 --> 00:06:06,700
So total number of time to rot.

118
00:06:06,700 --> 00:06:10,870
All the oranges is two minutes late.

119
00:06:10,870 --> 00:06:19,210
So this is very important to understand that in one minute, every rotten orange, every orange will

120
00:06:19,210 --> 00:06:24,660
rot its corresponding oranges horizontally, vertically connected, fresh oranges.

121
00:06:24,670 --> 00:06:25,090
Right.

122
00:06:26,020 --> 00:06:27,780
So that is very important to understand.

123
00:06:27,790 --> 00:06:37,060
I am repeating myself after every one minute, all the rotten oranges that you have will rot its corresponding

124
00:06:37,360 --> 00:06:40,390
fresh oranges horizontally, vertically connected fresh oranges.

125
00:06:40,900 --> 00:06:41,570
And it.

126
00:06:41,890 --> 00:06:48,790
So again, what we need to do, we need to find out the minimum number of time that we need to find

127
00:06:48,790 --> 00:06:50,590
out the minimum number of minutes.

128
00:06:51,130 --> 00:06:54,910
That is, we can use BFC algorithm.

129
00:06:54,910 --> 00:06:55,360
Right.

130
00:06:56,260 --> 00:07:00,670
We can use BFE to find out the minimum number of minutes.

131
00:07:01,770 --> 00:07:02,050
Right.

132
00:07:02,410 --> 00:07:03,920
So how it will work.

133
00:07:03,940 --> 00:07:05,050
Let's talk about this.

134
00:07:05,830 --> 00:07:12,030
This is to then when one then one one zero, then zero one one.

135
00:07:12,310 --> 00:07:20,590
So what I'm going to do, what I will do in the queue, I will push all the rotten oranges, I will

136
00:07:20,590 --> 00:07:22,650
push all the rotten oranges.

137
00:07:23,050 --> 00:07:24,550
So this is a rotten orange.

138
00:07:24,550 --> 00:07:25,720
So I will push to.

139
00:07:27,280 --> 00:07:33,040
That is, I will push the index zero zero and the time time is initially zero.

140
00:07:34,440 --> 00:07:36,190
So after one minute, what will happen?

141
00:07:36,220 --> 00:07:38,220
So this is the nod to.

142
00:07:39,480 --> 00:07:41,910
And initially, right, initially zero.

143
00:07:42,300 --> 00:07:44,510
So what will happen after one minute, what will happen?

144
00:07:45,180 --> 00:07:47,480
This will get done and this will get rotten.

145
00:07:47,880 --> 00:07:50,640
So this will become to this will become too.

146
00:07:51,180 --> 00:07:51,770
So, too.

147
00:07:51,780 --> 00:07:53,400
And how much time in one minute?

148
00:07:53,400 --> 00:07:56,950
Only late after one minute to two and one.

149
00:07:57,330 --> 00:07:57,810
Correct.

150
00:07:59,320 --> 00:08:04,180
Now again, these two will draw this one and this one.

151
00:08:04,800 --> 00:08:08,850
So again, this really will become two and two.

152
00:08:09,600 --> 00:08:11,160
So it will become two and two.

153
00:08:14,310 --> 00:08:20,670
So after one more minute, these two will write this one, so it will become two and three and this

154
00:08:20,830 --> 00:08:24,350
will come to after one more minute, these two will write this one.

155
00:08:24,660 --> 00:08:26,650
So this will become 24.

156
00:08:26,940 --> 00:08:28,410
And what is our answer?

157
00:08:28,530 --> 00:08:29,790
Our answer is for.

158
00:08:30,780 --> 00:08:31,190
Right.

159
00:08:31,530 --> 00:08:32,929
So that's how it will work.

160
00:08:34,610 --> 00:08:42,860
Very, very simple, so we can use algorithm, we can visualize that this basically is a graph.

161
00:08:43,659 --> 00:08:51,610
Eight and initially, what I will do, I will push all the rotten oranges in dequeue, so how we can

162
00:08:51,610 --> 00:08:55,230
solve this problem, do you need to do you need to copy the code from here?

163
00:08:55,990 --> 00:08:58,570
You need to copy the code for this problem.

164
00:08:58,840 --> 00:08:59,260
Right.

165
00:08:59,440 --> 00:09:03,220
And paste the code here and modify the code according to this problem.

166
00:09:03,520 --> 00:09:05,800
So BFW algorithm can be used here.

167
00:09:06,610 --> 00:09:09,610
So do give it a try rate.

168
00:09:09,820 --> 00:09:12,420
I will write the code for this problem in our next video.

169
00:09:13,150 --> 00:09:14,860
So this has allowed this device.

170
00:09:14,860 --> 00:09:16,150
I will see you in the next one.

171
00:09:16,180 --> 00:09:16,750
Thank you.

