Skip to content

Commit c6cb30e

Browse files
authored
Update 1105-filling-bookcase-shelves.js
1 parent 7b2e52e commit c6cb30e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1105-filling-bookcase-shelves.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,24 @@ const minHeightShelves = function(books, shelf_width) {
1818
}
1919
return dp[books.length]
2020
};
21+
22+
// another
23+
24+
/**
25+
* @param {number[][]} books
26+
* @param {number} shelf_width
27+
* @return {number}
28+
*/
29+
const minHeightShelves = function(books, shelf_width) {
30+
const n = books.length, dp = Array(1001).fill(Infinity)
31+
dp[0] = 0
32+
for(let i = 0; i < n; i++) {
33+
let sum = 0, mx = 0
34+
for(let j = i; j >= 0 && sum + books[j][0] <= shelf_width; j--) {
35+
sum += books[j][0]
36+
mx = Math.max(mx, books[j][1])
37+
dp[i + 1] = Math.min(dp[i + 1], dp[j] + mx)
38+
}
39+
}
40+
return dp[n]
41+
};

0 commit comments

Comments
 (0)