Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Filling Bookcase Shelves in Python
The bookshelf filling problem involves arranging books with given thickness and height onto shelves of limited width while maintaining their original order. We need to find the minimum possible total height of the bookcase.
Each book has dimensions books[i][0] (thickness) and books[i][1] (height). Books must be placed in the given order, and each shelf has a maximum width of shelf_width.
Algorithm Approach
We use dynamic programming where dp[i] represents the minimum height needed to place the first i+1 books. For each book, we consider all possible ways to place it on the current shelf ?
Implementation
class Solution:
def minHeightShelves(self, books, shelf_width):
"""
:type books: List[List[int]]
:type shelf_width: int
:rtype: int
"""
n = len(books)
dp = [float('inf')] * n
dp[0] = books[0][1] # First book height
for i in range(1, n):
current_height = 0
remaining_width = shelf_width
j = i
# Try placing books[j] to books[i] on the same shelf
while j >= 0 and remaining_width - books[j][0] >= 0:
current_height = max(books[j][1], current_height)
prev_height = dp[j-1] if j-1 >= 0 else 0
dp[i] = min(dp[i], current_height + prev_height)
remaining_width -= books[j][0]
j -= 1
return dp[-1]
# Test the solution
solution = Solution()
books = [[1,1], [2,3], [2,3], [1,1], [1,1], [1,1], [1,2]]
shelf_width = 4
result = solution.minHeightShelves(books, shelf_width)
print(f"Minimum bookshelf height: {result}")
Minimum bookshelf height: 6
How It Works
The algorithm works by considering all possible arrangements ?
-
Initialize: Create a DP array where
dp[i]stores minimum height for firsti+1books -
Base case:
dp[0] = books[0][1](height of first book) - For each book i: Try placing it with previous books on the same shelf
-
Check constraints: Ensure total width doesn't exceed
shelf_width - Update minimum: Choose the arrangement giving minimum total height
Example Walkthrough
books = [[1,1], [2,3], [2,3], [1,1], [1,1], [1,1], [1,2]]
shelf_width = 4
# Step-by-step DP calculation
solution = Solution()
# Simulate the DP process
dp = [float('inf')] * len(books)
dp[0] = books[0][1]
print("DP progression:")
print(f"dp[0] = {dp[0]} (first book height)")
for i in range(1, len(books)):
current_height = 0
remaining_width = shelf_width
j = i
print(f"\nProcessing book {i}: thickness={books[i][0]}, height={books[i][1]}")
while j >= 0 and remaining_width - books[j][0] >= 0:
current_height = max(books[j][1], current_height)
prev_height = dp[j-1] if j-1 >= 0 else 0
dp[i] = min(dp[i], current_height + prev_height)
remaining_width -= books[j][0]
j -= 1
print(f"dp[{i}] = {dp[i]}")
print(f"\nFinal minimum height: {dp[-1]}")
DP progression: dp[0] = 1 (first book height) Processing book 1: thickness=2, height=3 dp[1] = 4 Processing book 2: thickness=2, height=3 dp[2] = 4 Processing book 3: thickness=1, height=1 dp[3] = 5 Processing book 4: thickness=1, height=1 dp[4] = 5 Processing book 5: thickness=1, height=1 dp[5] = 5 Processing book 6: thickness=1, height=2 dp[6] = 6 Final minimum height: 6
Time Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time | O(n²) | For each book, we may check all previous books |
| Space | O(n) | DP array of size n |
Conclusion
The bookshelf filling problem uses dynamic programming to find optimal book arrangements. The key insight is trying all possible shelf configurations while maintaining book order and width constraints.
