Filling Bookcase Shelves - Complete Solution Guide
Filling Bookcase Shelves is LeetCode problem 1105, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array books where books[i] = [thickness i , height i ] indicates the thickness and height of the i th 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
Detailed Explanation
The problem asks us to arrange a given list of books onto shelves of a bookcase, where each shelf has a specified width. The goal is to minimize the total height of the bookcase. We must place the books in the order they appear in the input list. We can place as many books as possible on a shelf, as long as the sum of their thicknesses does not exceed the shelf width. The height of a shelf is determined by the tallest book placed on that shelf. The total bookcase height is the sum of the heights of each shelf. The input is a list of `books`, where each book is represented by its thickness and height, and an integer `shelfWidth`. The output is the minimum possible height of the bookcase.
Solution Approach
The provided code uses dynamic programming to solve this problem. It initializes a `dp` array where `dp[i]` stores the minimum height of the bookcase after placing the first `i` books. The code iterates through the books, and for each book `i`, it considers all possible starting points `j` for the last shelf (from `i-1` down to `0`). For each possible shelf starting point `j`, it calculates the width and maximum height of that shelf. If the shelf width is valid, it updates `dp[i]` with the minimum value between its current value and `dp[j] + max_height_on_shelf`. The final result is stored in `dp[n]`, which represents the minimum height after placing all `n` books.
Step-by-Step Algorithm
- Step 1: Initialize a `dp` array of size `n+1` with infinity, where `n` is the number of books. `dp[0]` is initialized to 0, representing the base case (no books, no height).
- Step 2: Iterate through the books from `i = 1` to `n` (inclusive). This represents placing the i-th book (books[i-1]).
- Step 3: For each book `i`, iterate backward from `j = i-1` down to `0`. This represents considering all possible starting points for the last shelf. The last shelf starts from books[j].
- Step 4: Calculate the current shelf width and maximum height on the shelf. Add the thickness of the current book `books[j]` to the `current_shelf_width` and update the `max_height_on_shelf` if necessary.
- Step 5: Check if the `current_shelf_width` exceeds the `shelfWidth`. If it does, break the inner loop, since adding more books to this shelf is not feasible.
- Step 6: If `dp[j]` is not infinity, update `dp[i]` with the minimum value between its current value and `dp[j] + max_height_on_shelf`. This represents the minimum height to shelve the first `i` books, considering the optimal height for the first `j` books plus the height of the current shelf.
- Step 7: After iterating through all possible starting points for each book `i`, `dp[i]` will store the minimum height needed to shelve the first `i` books.
- Step 8: Finally, return `dp[n]`, which contains the minimum height needed to shelve all `n` books.
Key Insights
- Insight 1: Dynamic Programming is suitable for this problem because we need to find the optimal (minimum) height among many possible shelf arrangements. We can break down the problem into smaller subproblems: finding the minimum height for the first `i` books.
- Insight 2: The order of books is fixed, simplifying the problem. We only need to decide where to start a new shelf.
- Insight 3: We need to iterate backward from the current book to consider all possible combinations of books on the current shelf. This involves calculating the current shelf's width and height based on the books placed on it.
- Insight 4: The `dp` array stores the minimum height for the first `i` books. `dp[i]` represents the minimum height needed to shelve the first `i` books.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Flipkart.