Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 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.
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.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 <= thicknessi <= shelfWidth <= 10001 <= heighti <= 1000Problem summary: 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 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 5 books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]] 4
[[1,3],[2,4],[3,2]] 6
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1105: Filling Bookcase Shelves
class Solution {
public int minHeightShelves(int[][] books, int shelfWidth) {
int n = books.length;
int[] f = new int[n + 1];
for (int i = 1; i <= n; ++i) {
int w = books[i - 1][0], h = books[i - 1][1];
f[i] = f[i - 1] + h;
for (int j = i - 1; j > 0; --j) {
w += books[j - 1][0];
if (w > shelfWidth) {
break;
}
h = Math.max(h, books[j - 1][1]);
f[i] = Math.min(f[i], f[j - 1] + h);
}
}
return f[n];
}
}
// Accepted solution for LeetCode #1105: Filling Bookcase Shelves
func minHeightShelves(books [][]int, shelfWidth int) int {
n := len(books)
f := make([]int, n+1)
for i := 1; i <= n; i++ {
w, h := books[i-1][0], books[i-1][1]
f[i] = f[i-1] + h
for j := i - 1; j > 0; j-- {
w += books[j-1][0]
if w > shelfWidth {
break
}
h = max(h, books[j-1][1])
f[i] = min(f[i], f[j-1]+h)
}
}
return f[n]
}
# Accepted solution for LeetCode #1105: Filling Bookcase Shelves
class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
n = len(books)
f = [0] * (n + 1)
for i, (w, h) in enumerate(books, 1):
f[i] = f[i - 1] + h
for j in range(i - 1, 0, -1):
w += books[j - 1][0]
if w > shelfWidth:
break
h = max(h, books[j - 1][1])
f[i] = min(f[i], f[j - 1] + h)
return f[n]
// Accepted solution for LeetCode #1105: Filling Bookcase Shelves
struct Solution;
impl Solution {
fn min_height_shelves(books: Vec<Vec<i32>>, shelf_width: i32) -> i32 {
let n = books.len();
let mut dp = vec![0; n + 1];
for i in 0..n {
let mut width = books[i][0];
let mut height = books[i][1];
dp[i + 1] = height + dp[i];
for j in (0..i).rev() {
width += books[j][0];
if width <= shelf_width {
height = height.max(books[j][1]);
dp[i + 1] = dp[i + 1].min(height + dp[j]);
}
}
}
dp[n]
}
}
#[test]
fn test() {
let books = vec_vec_i32![[1, 1], [2, 3], [2, 3], [1, 1], [1, 1], [1, 1], [1, 2]];
let shelf_width = 4;
let res = 6;
assert_eq!(Solution::min_height_shelves(books, shelf_width), res);
}
// Accepted solution for LeetCode #1105: Filling Bookcase Shelves
function minHeightShelves(books: number[][], shelfWidth: number): number {
const n = books.length;
const f = new Array(n + 1).fill(0);
for (let i = 1; i <= n; ++i) {
let [w, h] = books[i - 1];
f[i] = f[i - 1] + h;
for (let j = i - 1; j > 0; --j) {
w += books[j - 1][0];
if (w > shelfWidth) {
break;
}
h = Math.max(h, books[j - 1][1]);
f[i] = Math.min(f[i], f[j - 1] + h);
}
}
return f[n];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.