Filling Bookcase Shelves Problem
Description
LeetCode Problem 1105.
You are given an array books where books[i] = [thickness_i, height_i] indicates the thickness and height of the i^th book. You are also given an integer shelfWidth.
We want to place these books in order onto bookcase shelves that have a total width shelfWidth.
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.
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.
- For example, if we have an ordered list of
5books, 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.
Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.
Example 1:

Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelfWidth = 4
Output: 6
Explanation:
The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
Notice that book number 2 does not have to be on the first shelf.
Example 2:
Input: books = [[1,3],[2,4],[3,2]], shelfWidth = 6
Output: 4
Constraints:
1 <= books.length <= 10001 <= thickness_i <= shelfWidth <= 10001 <= height_i <= 1000
Solution
Approach: Dynamic Programming over Prefixes
The books keep their order, so every arrangement cuts the sequence into consecutive groups, one group per shelf. A shelf costs the height of its tallest book, and its total thickness must be at most shelfWidth.
Let f[i] be the smallest total height for the first i books, with f[0] = 0. In the best arrangement of these i books, the last shelf holds some books j-1 .. i-1, and the shelves below it are the best arrangement of the first j - 1 books. So:
f[i] = min over j of f[j - 1] + max(height of books j-1 .. i-1), for every j where those books fit on one shelf.
To compute this, start with the last shelf holding only book i-1 and grow it to the left one book at a time, keeping its running width and its running maximum height. As soon as the width goes over shelfWidth, stop: adding more books only makes it wider.
The greedy idea "fill each shelf as much as possible" does not work. In Example 1, putting the first book alone on a shelf is better, because then the two books of height 3 can share the next one.
Complexity: O(n · w) time, where w is the largest number of books that fit on one shelf (at most O(n2)), and O(n) space.
class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
n = len(books)
# f[i] = the smallest height for the first i books
f = [0] * (n + 1)
for i in range(1, n + 1):
# The last shelf holds books j-1 .. i-1; grow it to the left while it fits.
width, height = 0, 0
f[i] = float("inf")
for j in range(i, 0, -1):
width += books[j - 1][0]
if width > shelfWidth:
break
height = max(height, books[j - 1][1])
f[i] = min(f[i], f[j - 1] + height)
return f[n]