Skip to content

Commit eb625c2

Browse files
committed
feat: add No.1105
1 parent 614ac5c commit eb625c2

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 1105. Filling Bookcase Shelves
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an array `books` where `books[i] = [thicknessi, heighti]` indicates the thickness and height of the `ith` book. You are also given an integer `shelfWidth`.
10+
11+
We want to place these books in order onto bookcase shelves that have a total width `shelfWidth`.
12+
13+
We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to `shelfWidth`, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.
14+
15+
Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.
16+
17+
18+
19+
- For example, if we have an ordered list of `5` books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.
20+
21+
22+
Return **the minimum possible height that the total bookshelf can be after placing shelves in this manner**.
23+
24+
 
25+
Example 1:
26+
27+
![](https://assets.leetcode.com/uploads/2019/06/24/shelves.png)
28+
29+
```
30+
Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelfWidth = 4
31+
Output: 6
32+
Explanation:
33+
The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
34+
Notice that book number 2 does not have to be on the first shelf.
35+
```
36+
37+
Example 2:
38+
39+
```
40+
Input: books = [[1,3],[2,4],[3,2]], shelfWidth = 6
41+
Output: 4
42+
```
43+
44+
 
45+
**Constraints:**
46+
47+
48+
49+
- `1 <= books.length <= 1000`
50+
51+
- `1 <= thicknessi <= shelfWidth <= 1000`
52+
53+
- `1 <= heighti <= 1000`
54+
55+
56+
57+
## Solution
58+
59+
```javascript
60+
/**
61+
* @param {number[][]} books
62+
* @param {number} shelfWidth
63+
* @return {number}
64+
*/
65+
var minHeightShelves = function(books, shelfWidth) {
66+
var dp = Array(books.length).fill(0).map(() => Array(shelfWidth));
67+
var helper = function(i, rowWidthLeft, rowHeight) {
68+
if (i === books.length) return rowHeight;
69+
if (dp[i][rowWidthLeft - 1]) return dp[i][rowWidthLeft - 1];
70+
var res = Number.MAX_SAFE_INTEGER;
71+
// put current book on the same row
72+
if (books[i][0] <= rowWidthLeft) {
73+
res = Math.min(res, helper(i + 1, rowWidthLeft - books[i][0], Math.max(rowHeight, books[i][1])));
74+
}
75+
// put current book on the next row
76+
res = Math.min(res, rowHeight + helper(i + 1, shelfWidth - books[i][0], books[i][1]));
77+
dp[i][rowWidthLeft - 1] = res;
78+
return res;
79+
};
80+
return helper(0, shelfWidth, 0);
81+
};
82+
83+
84+
```
85+
86+
**Explain:**
87+
88+
nope.
89+
90+
**Complexity:**
91+
92+
* Time complexity : O(n * m).
93+
* Space complexity : O(n * m).

0 commit comments

Comments
 (0)