Painter's Partition Problem | DSA Placement Series..

  • 2 days ago
Painter's Partition Problem | DSA Placement Series..

Category

📚
Learning
Transcript
00:00Hi everyone and welcome to the complete DSA series in which we are going to do an important
00:08problem called the Painter's Partition Problem.
00:10Apart from this, if we want to learn any other concept of DSA, we will get it on this channel
00:14in this playlist.
00:15And also we have prepared a company-wise DSA sheet in which we will get the DSA questions
00:21along with the list of companies in which those questions have already been asked.
00:25You will also get the link of this sheet in the description box below.
00:28Now let's start with the problem.
00:30The problem is called Painter's Partition Problem and this question is a direct variation
00:35of book allocation problem which we covered in the last lecture.
00:40So if you have watched the last lecture, most probably we will solve this question ourselves.
00:43So the question states that we have N boards, each with a given length, which is given in
00:48the form of an array.
00:49For example, we have N is equal to 4 i.e. we have 4 boards.
00:53We can imagine these as wooden boards and the length of these 4 boards is given in the
00:58form of an array.
00:59For example, we have 4 boards and we take the length of all the 4 boards as 10 units.
01:05Along with this, we are given M painters and M painters are given in such a way that every
01:11painter paints 1 unit of board in 1 unit of time.
01:16For example, let's suppose we have 2 painters and the job of these painters is to paint
01:21our 4 boards.
01:22Now when they paint these 4 boards, then it will take them 1 unit to paint 1 unit of board.
01:28i.e. if the length of a board is 10, then they can also say that if our unit is 1 minute,
01:32then it will take them 10 minutes to paint this board.
01:35If the length is 7, then it will take them 7 minutes to paint that board and all painters
01:40paint the board with equal speed.
01:42The task is to find the minimum time to paint all the boards.
01:46We have to find out the minimum time in which we can paint all these boards using these
01:52M is equal to 2 painters, such that, there is a given condition that the painter will
01:57only paint continuous sections of the board, i.e. if the first board is painted by painter
02:021 and the second board is painted by painter 2, then painter 1 cannot paint the third board.
02:07The remaining boards will have to be painted by painter 2 only, i.e. the boards that the
02:12painters will get will be in a contiguous or continuous manner.
02:15So what will be the minimum time in which these 2 painters will paint all these boards?
02:19Obviously, we are getting to know that if we assign these 2 boards to one painter and
02:24these 2 boards to the second painter, both the painters will take 20-20 minutes to paint
02:29their boards, then our minimum value will be equal to 20.
02:33We cannot paint the boards in a value less than this, so this is going to be the answer.
02:38Now, whoever has made a book allocation problem, I would advise you to pause here and try to
02:42solve this question on your own because it is going to be very easy.
02:45Now, let's try to solve it through another example.
02:48Let's suppose we have this array of the length of our boards, n is equal to 4 value, m is
02:52equal to 2 painters.
02:54Now, binary search is not going to be the most intuitive approach on this question because
02:58neither an array, a sorted array will be given to us every time that we try to find a value
03:03in this array, so we will have to think of an alternate approach to solve this question.
03:08Basically, our problem is that we have 2 painters, p1 and p2, and our task is to allocate all
03:15of these boards among these 2 painters.
03:18Now, for the solution, we basically need to find time to paint the boards and we have to
03:24find out the minimum value of this thing.
03:26Now, how can we finish our painting work?
03:30The first method would be that we assign this board to painter 1 and assign the remaining
03:34boards to painter 2.
03:36So, how much time will painter 1 take?
03:3840 minutes.
03:39Painter 2 will have to paint all the boards, so it will take time 30 plus 10 plus 20, i.e.
03:43equal to 60 minutes.
03:46The second case can be that we assign 2 boards to painter 1 and 2 boards to painter 2, so
03:53the first painter will take 70 minutes, the second painter will take 30 minutes, the third
03:58case can be that we assign 3 boards to painter 1 and 2 boards to painter 2, so the painter
04:051 will take 80 minutes and the painter 2 will take 20 minutes.
04:09Now, there can be another case where we assign all the boards to the same painter, but we
04:14will not be going towards the minimum value of time.
04:18Why?
04:19Because we will have one painter who has not been assigned any job, painter 1 has to do
04:22all this work.
04:23So, obviously, if only one painter does all the work, then the time will increase, it
04:26can never decrease.
04:27So, these are the 3 logical cases from which we can get our minimum time.
04:31Now, if we talk about the first case, painter 1 is taking 40 minutes, painter 2 is taking
04:3460 minutes.
04:35So, how much time will it take to finish the work?
04:37In the first case, it will definitely take 60 minutes to finish the work, in the second
04:40case, painter 1 is taking 70 minutes, painter 2 is taking 30 minutes, but it will take 70
04:44minutes to finish the work.
04:45In the third case, it will take 80 minutes to finish the work.
04:49In which case has the work finished within the minimum time?
04:52Definitely the first case, in which we did the partition from here, we did the partition
04:55from here.
04:56We assigned these boards to painter 1 and we assigned the remaining boards to painter
05:002.
05:01So, this is the minimum time taken to paint all of the boards and this should be our answer,
05:06we have to return the value of 60.
05:08Now, our approach to solve the question will be that if we have to find out the minimum
05:15time taken to paint all the boards, then why not take out all the possible values of the
05:20time taken to paint the boards.
05:23That is, if we have these boards, then to paint these boards, can we take the minimum
05:29time and can we take the maximum time?
05:33If we take out the value of minimum and maximum time logically, then the answer will be within
05:38this range.
05:39So, how much time will it take us to paint the boards?
05:44The maximum time taken to paint a given board will take us to paint all the boards.
05:50We will assign only one painter.
05:51So, what will the painter do?
05:52First, he will paint this, then he will do this, then he will do this, then he will do
05:53this.
05:54In that case, we will take the maximum time.
05:55So, what is the value of the maximum time that we can take to paint all these boards?
06:01That is going to be 40 plus 30 plus 10 plus 20, which is equal to 100 units.
06:06Can we say that this maximum time is also equal to the sum of all the board lengths?
06:12If we take out the sum of the length of all the boards, then what will happen?
06:16That will be our maximum time.
06:17It cannot take more time to paint all the boards than this.
06:20And what will be the minimum time?
06:22The minimum time logically will be that we assign a different painter to paint each board.
06:26Definitely, we have a limit of painters.
06:29And if we hypothetically think that a painter will paint this board, he will do this, he
06:33will do this, he will do this, he will do this, he will do this, he will do this, he
06:35will do this, he will do this, he will do this, he will do this, he will do this, he
06:36will do this, he will do this, he will do this, he will do this, he will do this, he
06:37will do this, he will do this, he will do this, he will do this, he will do this, he
06:38will do this, he will do this, he will do this, he will do this, he will do this, he
06:39will do this, he will do this, he will do this, he will do this, he will do this, he
06:40will do this, he will do this, he will do this, he will do this, he will do this, he
06:41will do this, he will do this, he will do this, he will do this, he will do this, he
07:02will do this, he will do this, he will do this, he will do this, he will do this, he
07:30will do this, he will do this, he will do this, he will do this, he will do this, he
07:59will do this, he will do this, he will do this, he will do this, he will do this, he
08:25will do this, he will do this, he will do this, he will do this, he will do this, he
08:54will do this, he will do this, he will do this, he will do this, he will do this, he
09:20will do this, he will do this, he will do this, he will do this, he will do this, he
09:49will do this, he will do this, he will do this, he will do this, he will do this, he
10:18will do this, he will do this, he will do this, he will do this, he will do this, he will do
10:47this, he will do this, he will do this, he will do this, he will do this, he will do this,
11:15he will do this, he will do this, he will do this, he will do this, he will do this, he
11:41will do this, he will do this, he will do this, he will do this, he will do this, he will do
12:08this, he will do this, he will do this, he will do this, he will do this, he will do this, he will
12:38do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do
13:04this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do
13:30this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do
14:00this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this, he will do this
14:30he will do this, he will do this, he will do this, he will do this, he will do this, he
14:37will do this, he will do this, he will do this, he will do this, he will do this, he
14:44will do this, he will do this, he will do this, he will do this, he will do this, he
14:51will do this, he will do this, he will do this, he will do this, he will do this, he
14:58will do this, he will do this, he will do this, he will do this, he will do this, he
15:05will do this, he will do this, he will do this, he will do this, he will do this, he
15:10will do this, he will do this, he will do this, he will do this, he will do this, he
15:15will do this, he will do this, he will do this, he will do this, he will do this, he
15:20will do this, he will do this, he will do this, he will do this, he will do this, he
15:25will do this, he will do this, he will do this, he will do this, he will do this, he
15:30will do this, he will do this, he will do this, he will do this, he will do this, he
15:35will do this, he will do this, he will do this, he will do this, he will do this, he
15:40will do this, he will do this, he will do this, he will do this, he will do this, he
15:45will do this, he will do this, he will do this, he will do this, he will do this, he
15:50will do this, he will do this, he will do this, he will do this, he will do this, he
15:55will do this, he will do this, he will do this, he will do this, he will do this, he
16:00will do this, he will do this, he will do this, he will do this, he will do this, he
16:05will do this, he will do this, he will do this, he will do this, he will do this, he
16:10will do this, he will do this, he will do this, he will do this, he will do this, he
16:15will do this, he will do this, he will do this, he will do this, he will do this, he
16:20will do this, he will do this, he will do this, he will do this, he will do this, he
16:25will do this, he will do this, he will do this, he will do this, he will do this, he
16:30will do this, he will do this, he will do this, he will do this, he will do this, he
16:35will do this, he will do this, he will do this, he will do this, he will do this, he
16:40will do this, he will do this, he will do this, he will do this, he will do this, he
16:45will do this, he will do this, he will do this, he will do this, he will do this, he
16:50will do this, he will do this, he will do this, he will do this, he will do this, he
16:55will do this, he will do this, he will do this, he will do this, he will do this, he
17:00will do this, he will do this, he will do this, he will do this, he will do this, he
17:05will do this, he will do this, he will do this, he will do this, he will do this, he
17:10will do this, he will do this, he will do this, he will do this, he will do this, he
17:15will do this, he will do this, he will do this, he will do this, he will do this, he
17:20will do this, he will do this, he will do this, he will do this, he will do this, he
17:25will do this, he will do this, he will do this, he will do this, he will do this, he
17:30will do this, he will do this, he will do this, he will do this, he will do this, he
17:35will do this, he will do this, he will do this, he will do this, he will do this, he
17:40will do this, he will do this, he will do this, he will do this, he will do this, he
17:45will do this, he will do this, he will do this, he will do this, he will do this, he
17:50will do this, he will do this, he will do this, he will do this, he will do this, he
17:55will do this, he will do this, he will do this, he will do this, he will do this, he
18:00will do this, he will do this, he will do this, he will do this, he will do this, he
18:05will do this, he will do this, he will do this, he will do this, he will do this, he
18:10will do this, he will do this, he will do this, he will do this, he will do this, he
18:15will do this, he will do this, he will do this, he will do this, he will do this, he
18:20will do this, he will do this, he will do this, he will do this, he will do this, he
18:25will do this, he will do this, he will do this, he will do this, he will do this, he
18:30will do this, he will do this, he will do this, he will do this, he will do this, he
18:35will do this, he will do this, he will do this, he will do this, he will do this, he
18:40will do this, he will do this, he will do this, he will do this, he will do this, he
18:45will do this, he will do this, he will do this, he will do this, he will do this, he
18:50will do this, he will do this, he will do this, he will do this, he will do this, he
18:55will do this, he will do this, he will do this, he will do this, he will do this, he
19:00will do this, he will do this, he will do this, he will do this, he will do this, he
19:05will do this, he will do this, he will do this, he will do this, he will do this, he
19:10will do this, he will do this, he will do this, he will do this, he will do this, he
19:15will do this, he will do this, he will do this, he will do this, he will do this, he
19:20will do this, he will do this, he will do this, he will do this, he will do this, he
19:25will do this, he will do this, he will do this, he will do this, he will do this, he
19:30will do this, he will do this, he will do this, he will do this, he will do this, he
19:35will do this, he will do this, he will do this, he will do this, he will do this, he
19:40will do this, he will do this, he will do this, he will do this, he will do this, he
19:45will do this, he will do this, he will do this, he will do this, he will do this, he
19:50will do this, he will do this, he will do this, he will do this, he will do this, he
19:55will do this, he will do this, he will do this, he will do this, he will do this, he
20:00will do this, he will do this, he will do this, he will do this, he will do this, he
20:05will do this, he will do this, he will do this, he will do this, he will do this, he
20:10will do this, he will do this, he will do this, he will do this, he will do this, he
20:15will do this, he will do this, he will do this, he will do this, he will do this, he
20:20will do this, he will do this, he will do this, he will do this, he will do this, he
20:25will do this, he will do this, he will do this, he will do this, he will do this, he
20:30will do this, he will do this, he will do this, he will do this, he will do this, he
20:35will do this, he will do this, he will do this, he will do this, he will do this, he
20:40will do this, he will do this, he will do this, he will do this, he will do this, he
20:45will do this, he will do this, he will do this, he will do this, he will do this, he
20:50will do this, he will do this, he will do this, he will do this, he will do this, he
20:55will do this, he will do this, he will do this, he will do this, he will do this, he
21:00will do this, he will do this, he will do this, he will do this, he will do this, he
21:05will do this, he will do this, he will do this, he will do this, he will do this, he
21:10will do this, he will do this, he will do this, he will do this, he will do this, he
21:15will do this, he will do this, he will do this, he will do this, he will do this, he
21:20will do this, he will do this, he will do this, he will do this, he will do this, he
21:25will do this, he will do this, he will do this, he will do this, he will do this, he
21:30will do this, he will do this, he will do this, he will do this, he will do this, he
21:35will do this, he will do this, he will do this, he will do this, he will do this, he
21:40will do this, he will do this, he will do this, he will do this, he will do this, he
21:45will do this, he will do this, he will do this, he will do this, he will do this, he
21:50will do this, he will do this, he will do this, he will do this, he will do this, he
21:55will do this, he will do this, he will do this, he will do this, he will do this, he
22:00will do this, he will do this, he will do this, he will do this, he will do this, he
22:05will do this, he will do this, he will do this, he will do this, he will do this, he
22:10will do this, he will do this, he will do this, he will do this, he will do this, he
22:15will do this, he will do this, he will do this, he will do this, he will do this, he
22:20will do this, he will do this, he will do this, he will do this, he will do this, he
22:25will do this, he will do this, he will do this, he will do this, he will do this, he
22:30will do this, he will do this, he will do this, he will do this, he will do this, he
22:34will do this, he will do this, he will do this, he will do this, he will do this, he
22:37will do this, he will do this, he will do this, he will do this, he will do this, he
22:40will do this, he will do this, he will do this, he will do this, he will do this, he
22:43will do this, he will do this, he will do this, he will do this, he will do this, he
22:46will do this, he will do this, he will do this, he will do this, he will do this, he
22:49will do this, he will do this, he will do this, he will do this, he will do this, he
22:51will do this, he will do this, he will do this, he will do this, he will do this, he
22:53will do this, he will do this, he will do this, he will do this, he will do this, he
22:56will do this, he will do this, he will do this, he will do this, he will do this, he
22:59will do this, he will do this, he will do this, he will do this, he will do this, he
23:02will do this, he will do this, he will do this, he will do this, he will do this, he
23:04will do this, he will do this, he will do this, he will do this, he will do this, he
23:06will do this, he will do this, he will do this, he will do this, he will do this, he
23:07will do this, he will do this, he will do this, he will do this, he will do this, he
23:08will do this, he will do this, he will do this, he will do this, he will do this, he
23:09will do this, he will do this, he will do this, he will do this, he will do this, he
23:11will do this, he will do this, he will do this, he will do this, he will do this, he
23:14will do this, he will do this, he will do this, he will do this, he will do this, he
23:15will do this, he will do this, he will do this, he will do this, he will do this, he
23:16will do this, he will do this, he will do this, he will do this, he will do this, he
23:17will do this, he will do this, he will do this, he will do this, he will do this, he
23:18will do this, he will do this, he will do this, he will do this, he will do this, he
23:19will do this, he will do this, he will do this, he will do this, he will do this, he
23:20will do this, he will do this, he will do this, he will do this, he will do this, he
23:21will do this, he will do this, he will do this, he will do this, he will do this, he
23:22will do this, he will do this, he will do this, he will do this, he will do this, he
23:23will do this, he will do this, he will do this, he will do this, he will do this, he
23:24will do this, he will do this, he will do this, he will do this, he will do this, he
23:25will do this, he will do this, he will do this, he will do this, he will do this, he
23:26will do this, he will do this, he will do this, he will do this, he will do this, he
23:27will do this, he will do this, he will do this, he will do this, he will do this, he
23:28will do this, he will do this, he will do this, he will do this, he will do this, he
23:29will do this, he will do this and he will do this.
23:58with that we will check that our mid value is it a possible answer, so for possible answer
24:06we will pass our array n, m and mid, if it is possible answer then we will save this answer,
24:14if it is possible then we will save mid as answer and with this we will try to find minimum
24:18value for which end will be mid-1 and if it is not possible answer then in that case in
24:25second half we will go to left and here we will go to right, so here it will start mid
24:32plus 1 is equal and in last we will return our possible answer, now we just have to complete
24:37this function which is a boolean function whose name is isPossible, this function will
24:44finally tell us that any mid is a possible answer or not, now here we will say this mid
24:51is a possible answer or not, if it is possible answer then we will return mid-1, if it is
24:58not possible answer then we will return mid-2, if it is possible answer then we will return
25:05mid-3, if it is possible answer then we will return mid-4, if it is possible answer then
25:12we will return mid-5, if it is possible answer then we will return mid-6, if it is possible
25:19answer then we will return mid 6, if it is able answer then we will return midt 7, if
25:27it is not possible answer then return mid 8 plus 1 and finally if it is possible answer
25:34for which we will make painters++ and reinitialize time to the current board
25:39array of i.
25:40at last we will check whether the value of painters is less than or equal to m
25:47if it is less than or equal to m then we have to return true
25:50if it is not then we have to return false
25:52so simply we can return this value from here
25:54so this is our overall code to solve the painters partition problem
25:59now if we talk about the time complexity of this code
26:02is a possible function
26:11so this function is big of n
26:13minimum time to paint
26:26can we say that this is approximately equal to
26:32log of sum
26:44for simplicity we call it log of sum
26:47because in this binary search loop
26:49we are calling this possible function
26:52whose time complexity is big of n
26:54so the overall time complexity of this loop will be log of sum into n
27:00so this will be big o of log of sum into n
27:04and this is the overall time complexity
27:06which is going to be of this function
27:08so the overall time complexity of our code is big o of n into log of sum
27:13let's save this
27:14for our values we will find out our answer
27:18and the answer is going to be equal to 60
27:20so this is how we solve the painters partition problem using binary search
27:23apart from this if we want to solve more binary search questions
27:25then we will get it in the company wise dsa sheet below
27:29link is given in the description box below
27:31if you have successfully completed the lecture
27:33you can mark your attendance in the comments and write completed
27:35and you can also share your progress with me on twitter
27:37whose link is also given in the description box below
27:40that's all for today
27:41see you in the next lecture
27:42till then keep learning and keep exploring

Recommended