Skip to content

Commit 42995ca

Browse files
authored
Create 1105. Filling Bookcase Shelves
1 parent ed5ceb9 commit 42995ca

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

1105. Filling Bookcase Shelves

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
int n,w;
4+
vector<vector<int>> a;
5+
vector<int> dp;
6+
int ans=INT_MAX;
7+
int f(int i)
8+
{
9+
if (i>=n)
10+
return 0;
11+
if(dp[i]!=INT_MAX)
12+
return dp[i];
13+
// int a=INT_MAX;
14+
int cw=0,ch=0;
15+
for(int j=i;j<n;j++)
16+
{
17+
if(cw+a[j][0]<=w)
18+
{
19+
dp[i]=min(dp[i],max(ch,a[j][1])+f(j+1));
20+
ch=max(ch,a[j][1]);
21+
cw+=a[j][0];
22+
}
23+
else
24+
break;
25+
26+
}
27+
return dp[i];
28+
29+
}
30+
int minHeightShelves(vector<vector<int>>& books, int w) {
31+
32+
33+
this->n=books.size();
34+
this->a=books;
35+
this->w=w;
36+
dp.resize(n,INT_MAX);
37+
return f(0);
38+
39+
}
40+
};

0 commit comments

Comments
 (0)