Filling bookcase shelves problem using dynamic programming
ฝัง
- เผยแพร่เมื่อ 18 ก.ย. 2024
- Welcome to the video of Joey'sTech on a leetcode problem#1105 which is the filling bookcase shelves problem.
Now, this DP problem has the same difficulty level as the Box Stacking problem and Text Justification problem so, make sure you pay attention while watching this video.
I have done my best to keep you engaged with the explanation of this dynamic programming problem, so, you will have fun while learning to construct the dynamic programming
algorithm of the filling bookcase shelves problem.
I am saying this the first time since I started producing videos but watching this video of my dynamic programming tutorial will beneficial for you.
Watching it twice will unlock more thinking doors in the mind.
-------------------- Problem Statement ---------------------
You got some books to store in a bookshelf. Assume that you have to build this bookshelf to store your books.
You have to store books in the same order given in the question.
There will be multiple book cases in this bookshelf, once they get full you need to construct a book case beneath it.
The width of each book case is given to you in the variable shelf-width.
Each book given to you has 2 attributes denoted by book[i][0] and book[0][i]
book[i][0] represents the thickness of the book and book[0][i] represents its height.
You can only place those books in the bookshelf whose combined thickness is less than or equal to the shelf_width variable.
Like I mentioned earlier, once a book case is full you need to build another level of bookshelf whose height will be the maximum height of the books
placed on that level
You task is to build the bookshelf with the minimum height possible by fitting all the books in the racks.
This means you need to find the minimum height of the bookshelf.
Below is the book array and the value of shelf_width
[ [ 1,1 ] , [ 2,3 ] , [ 2, 3 ], [ 1,1 ], [ 1,1 ], [ 1,1 ], [ 1,2 ] ]
shelf_width = 4
So, now let's solve this problem with the classic dynamic programming technique in this video.
---------------------------- Also Watch -------------------------
Text justification problem dynamic programming
• Step by step guide to ...
Box stacking problem dynamic programming
• Box stacking problem u...
subset sum problem dynamic programming
• Subset sum problem | T...
Thanks
Glad I could help.
Thank you my man, I searched up so many algorithms but I couldn't fully understand them, and you totally nailed it!
Thanks Andrew for appreciating,,don't forget to subscribe.
Very good explanation. Only channel which covered this problem.
Thanks - do recommend joeystech to others
Thanks for covering such difficult level questions with detailed explanation.
Thanks for being a regular viewer
Very helpful video, you explained it very well.
Thanks Anurag for appreciating the effort.
I liked the video, thank you very much, the demonstration helped me!
You are welcome and don’t forget to subscribe
Great explanation!
Thank you Aanchal for the motivation
Bottom up dp of this guy is awesome
Hey Anish, if ‘this guy’ is me then thank you very much
@@joeystechpluscode yes it's you only I have seen whole dp playlist it's awesome
@@anishsuman1371 thanks man...Good luck!
awesome explanation
Thanks for appreciating
Java code :
class Solution {
public int minHeightShelves(int[][] books, int shelfWidth) {
// for every book i, we try to figure out how many prev books can be kept in same shelf including book i
int n=books.length;
//dp[i] denotes minHeightShelves if we take first i books into consideration
int[] dp=new int[n+1];
for(int i=1;ishelfWidth)
break;
dp[i]=Math.min(dp[i],height+dp[j-1]);
}
}
return dp[n];
}
}
great one buddy keep going
Thanks Samkit for the appreciation
this method could never occur to me.. i was trying to memoize the problem with regular approach which was making it too complicated. Thanks for the video !
Glad I could help
Am i only one unable to understand Problem statement ?
Hi
Hello
You people can not solve recursively you are smarter guys