Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Filling Bookcase Shelves

Posted on August 5, 2019July 26, 2020 by braindenny

Filling Bookcase Shelves



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #dynamicprogramming, #multiplechoices

We have a sequence of books: the i-th book has thickness books[i][0] and height books[i][1].

We want to place these books in order onto bookcase shelves that have total width shelf_width.

We choose some of the books to place on this shelf (such that the sum of their thickness is <= shelf_width), then build another level of 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 again 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 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:
Filling Bookcase Shelves

Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4
Output: 6
Explanation:
The sum of the heights of the 3 shelves are 1 + 3 + 2 = 6.
Notice that book number 2 does not have to be on the first shelf.

Constraints:

  • 1 <= books.length <= 1000
  • 1 <= books[i][0] <= shelf_width <= 1000
  • 1 <= books[i][1] <= 1000

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution: dp with padding element as base condition
// https://code.dennyzhang.com/filling-bookcase-shelves
// Basic Ideas: dynamic programming
//
//  dp(i): min height, with books[i] taken
//       // new row
//       // add to previous row
//
//  Follow-up: why binarysearch won't work?
//
// Complexity: Time O(n*m), Space O(n)
func minHeightShelves(books [][]int, shelf_width int) int {
    dp := make([]int, len(books)+1)
    for i:=1; i<len(dp); i++ {
        // dp[i] <--> books[i-1]
        // add to a new row
        dp[i] = dp[i-1]+books[i-1][1]
        // add to previous row
        width, height := books[i-1][0], books[i-1][1]
        for j:=i-1; j>0 && width+books[j-1][0] <= shelf_width; j-- {
            width = width+books[j-1][0]
            if books[j-1][1] > height {
                height = books[j-1][1]
            }
            if dp[i] > dp[j-1] + height {
                dp[i] = dp[j-1] + height
            }
        }
    }
    return dp[len(dp)-1]
}
  • Solution: dp without padding element as base condition
// https://code.dennyzhang.com/filling-bookcase-shelves
// Basic Ideas: dynamic programming
//
//  dp(i): min height, with books[i] taken
//       // new row
//       // add to previous row
//
//  Follow-up: why binarysearch won't work?
//
// Complexity: Time O(n*m), Space O(n)
func minHeightShelves(books [][]int, shelf_width int) int {
    dp := make([]int, len(books))
    dp[0] = books[0][1]
    for i:=1; i<len(dp); i++ {
        // add to a new row
        dp[i] = dp[i-1]+books[i][1]
        // add to previous row
        // books[i-1], books[i-2], ... books[i-X]
        width, height := books[i][0], books[i][1]
        for j:=i-1; j>=0 && width+books[j][0] <= shelf_width; j-- {
            width = width+books[j][0]
            if books[j][1] > height {
                height = books[j][1]
            }
            // collect result
            prev := 0
            if j > 0 {
                prev = dp[j-1]
            }
            if dp[i] > prev + height {
                dp[i] = prev + height
            }
        }
    }
    return dp[len(dp)-1]
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #dynamicprogramming, #inspiring, multiplechoices

Post navigation

a
Series: Multiple Choices At Each Step Problems & Follow-Up

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.