books[i] = [thickness, height]. Every shelf is width wide: the total thickness of the books on one shelf cannot exceed it.
The books go in strictly in the given order: the first few books on the top shelf, the next few on the shelf below, and so on. A shelf is as tall as the tallest book on it, and the bookcase is as tall as all its shelves together.
Return the smallest possible height of the bookcase.books=[[2, 5], [1, 2], [3, 2], [2, 6]], width=4 → 13
Shelf one: books 0 and 1 (thickness 3, height 5). Shelf two: book 2 (height 2). Shelf three: book 3 (height 6). Total 13. Putting books 1 and 2 together leaves book 0 alone, giving the same total; nothing does better.
books=[[1, 4], [1, 4], [1, 4]], width=3 → 4
All three books fit on one shelf of height 4.
books=[[3, 1], [1, 9], [3, 1], [1, 1]], width=4 → 10
books) ≤ 1000width ≤ 1000books=[[2, 5], [1, 2], [3, 2], [2, 6]], width=4
13
Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.