We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f21d700 commit fad5e45Copy full SHA for fad5e45
C++/filling-bookcase-shelves.cpp
@@ -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