Skip to content

Commit 4196f62

Browse files
authored
Update 1105-filling-bookcase-shelves.js
1 parent c6cb30e commit 4196f62

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1105-filling-bookcase-shelves.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,27 @@ const minHeightShelves = function(books, shelf_width) {
3939
}
4040
return dp[n]
4141
};
42+
43+
// another
44+
45+
/**
46+
* @param {number[][]} books
47+
* @param {number} shelf_width
48+
* @return {number}
49+
*/
50+
const minHeightShelves = function(books, shelf_width) {
51+
const n = books.length, dp = Array(1001)
52+
dp[0] = 0
53+
54+
for(let i = 0; i < n; i++) {
55+
let [w, h] = books[i]
56+
dp[i + 1] = dp[i] + h
57+
for(let j = i - 1; j >= 0 && w + books[j][0] <= shelf_width; j--) {
58+
h = Math.max(h, books[j][1])
59+
w += books[j][0]
60+
dp[i + 1] = Math.min(dp[i + 1], dp[j] + h)
61+
}
62+
}
63+
64+
return dp[n]
65+
};

0 commit comments

Comments
 (0)