#1105

Filling Bookcase Shelves

pupil · 585 · lc medium +29 · verified · 68.6% accepted · 2,675 likes · top 76%

Description

Books must be placed in order on shelves of width shelfWidth. Each books[i] = [thicknessi, heighti]. Books on the same shelf must fit within shelfWidth total thickness; the shelf height equals the tallest book on it.

Return the minimum total height of the bookcase.

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

Code

1
2
3