Filling Bookcase Shelves - Problem

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.

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 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.

Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.

Input & Output

Example 1 — Basic Case
$ Input: books = [[1,1],[2,3],[2,1],[1,3]], shelfWidth = 4
Output: 6
💡 Note: Optimal arrangement: Shelf 1: [1,1],[2,3] (width=3, height=3), Shelf 2: [2,1] (width=2, height=1), Shelf 3: [1,3] (width=1, height=3). Total height = 3+1+3 = 6.
Example 2 — All Books on One Shelf
$ Input: books = [[1,3],[2,4],[3,2]], shelfWidth = 6
Output: 4
💡 Note: All books fit on one shelf: total width = 1+2+3 = 6 ≤ 6, height = max(3,4,2) = 4.
Example 3 — Each Book Separate
$ Input: books = [[3,5],[3,4],[3,3]], shelfWidth = 3
Output: 12
💡 Note: Each book must be on its own shelf since each has width 3. Total height = 5+4+3 = 12.

Constraints

  • 1 ≤ books.length ≤ 1000
  • 1 ≤ thicknessi ≤ shelfWidth ≤ 1000
  • 1 ≤ heighti ≤ 1000

Visualization

Tap to expand
Filling Bookcase Shelves - DP Approach INPUT books = [[1,1],[2,3],[2,1],[1,3]] [thickness, height] B0 [1,1] B1 [2,3] B2 [2,1] B3 [1,3] shelfWidth = 4 |--1--|--2--|--3--|--4--| Input Values: n = 4 books shelfWidth = 4 ALGORITHM STEPS 1 Define DP State dp[i] = min height for books 0..i-1 2 Base Case dp[0] = 0 (no books) 3 Transition Try all valid shelf combos 4 Return dp[n] Minimum total height DP Table: i 0 1 2 3 4 dp[i] 0 1 3 4 6 dp[i] = min(dp[j] + maxH) for all j where width fits Shelf choices explored [B0,B1,B2] + [B3] or [B0] + [B1,B2,B3]... FINAL RESULT Optimal Arrangement: Shelf 1 (height=3) B0 B1 width: 1+2=3 Shelf 2 (height=3) B2 B3 width: 2+1=3 3 3 6 Output: 6 Minimum Height 3 + 3 = 6 OK - Verified Key Insight: For each book i, we try placing it on a new shelf or combining it with previous books on the current shelf. dp[i] stores the minimum height to place books 0 to i-1. We look back to find the best split point j where books j to i-1 can fit on one shelf (total width <= shelfWidth), minimizing dp[j] + max_height(j..i-1). TutorialsPoint - Filling Bookcase Shelves | Dynamic Programming Approach Time: O(n * shelfWidth) | Space: O(n)
Asked in
Amazon 8 Google 6 Microsoft 4 Facebook 3
95.0K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen