Skip to content

Commit fad5e45

Browse files
authored
Create filling-bookcase-shelves.cpp
1 parent f21d700 commit fad5e45

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

C++/filling-bookcase-shelves.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n^2)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int minHeightShelves(vector<vector<int>>& books, int shelf_width) {
7+
vector<int> dp(books.size() + 1, numeric_limits<int>::max());
8+
dp[0] = 0;
9+
for (int i = 1; i <= books.size(); ++i) {
10+
int max_width = shelf_width;
11+
int max_height = 0;
12+
for (int j = i - 1; j >= 0; --j) {
13+
if (max_width - books[j][0] < 0) {
14+
break;
15+
}
16+
max_width -= books[j][0];
17+
max_height = max(max_height, books[j][1]);
18+
dp[i] = min(dp[i], dp[j] + max_height);
19+
}
20+
}
21+
return dp[books.size()];
22+
}
23+
};

0 commit comments

Comments
 (0)