Skip to content

Commit

Permalink
Create 1105. Filling Bookcase Shelves (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Jul 31, 2024
2 parents ed5ceb9 + 42995ca commit 080b638
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 1105. Filling Bookcase Shelves
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
int n,w;
vector<vector<int>> a;
vector<int> dp;
int ans=INT_MAX;
int f(int i)
{
if (i>=n)
return 0;
if(dp[i]!=INT_MAX)
return dp[i];
// int a=INT_MAX;
int cw=0,ch=0;
for(int j=i;j<n;j++)
{
if(cw+a[j][0]<=w)
{
dp[i]=min(dp[i],max(ch,a[j][1])+f(j+1));
ch=max(ch,a[j][1]);
cw+=a[j][0];
}
else
break;

}
return dp[i];

}
int minHeightShelves(vector<vector<int>>& books, int w) {


this->n=books.size();
this->a=books;
this->w=w;
dp.resize(n,INT_MAX);
return f(0);

}
};

0 comments on commit 080b638

Please sign in to comment.