Similar Problems

Similar Problems not available

Filling Bookcase Shelves - Leetcode Solution

Companies:

LeetCode:  Filling Bookcase Shelves Leetcode Solution

Difficulty: Medium

Topics: dynamic-programming array  

Problem Statement:

You have a number of identical bookcases that you want to place in a room. Each bookcase has a certain height, width, and number of shelves. You also have a collection of books that you want to place on the bookcases, each with a certain height and width. You want to stack the books on the shelves of the bookcases, with the following constraints:

  1. Each book must be placed on a single shelf of a single bookcase.
  2. The spine of each book must be perpendicular to the shelf on which it is placed (i.e., the book must lie flat on the shelf).
  3. The height of the books on each shelf must be such that they do not exceed the height of the shelf.
  4. The width of the books on each shelf must be such that they do not exceed the width of the shelf.

Write a function to determine the minimum number of bookcases required to store all of the books. If it is not possible to store all of the books in the bookcases, return -1.

Solution:

This problem can be solved using a greedy approach. We can start by sorting the books in decreasing order of height. Then, we can iterate through the books and place them on the first available shelf in the first bookcase that can accommodate them. If no bookcase can accommodate the current book, we add a new bookcase and place the book on the first shelf.

To check if a book can be placed on a shelf, we need to keep track of the remaining height and width of each shelf. If the height of the book is less than or equal to the remaining height of the shelf and the width of the book is less than or equal to the remaining width of the shelf, the book can be placed on that shelf.

If, after placing all the books, there are still shelves that are empty, we can remove those shelves and reduce the number of bookcases accordingly.

If, after placing all the books, there are still books that have not been placed, it means that it is not possible to store all the books in the given bookcases. In this case, we return -1.

Here is the Python code that implements this approach:

def fill_bookcase_shelves(bookcases, books): books.sort(reverse=True) # sort books in decreasing order of height

for book in books:
    placed = False
    for i, shelf in enumerate(bookcases):
        if book.width <= shelf.width and book.height <= shelf.height:
            shelf.place_book(book)
            placed = True
            break
    if not placed:
        new_shelf = Shelf(bookcases[0].width, bookcases[0].height)
        new_shelf.place_book(book)
        bookcases.append(new_shelf)

# remove empty shelves
for shelf in bookcases:
    if shelf.num_books == 0:
        bookcases.remove(shelf)

# check if all books have been placed
for book in books:
    if not book.is_placed:
        return -1

return len(bookcases)

The Shelf class can be implemented as follows:

class Shelf: def init(self, width, height): self.width = width self.height = height self.remaining_width = width self.remaining_height = height self.books = []

def place_book(self, book):
    book.is_placed = True
    book.shelf = self
    self.books.append(book)
    self.remaining_height -= book.height
    self.remaining_width -= book.width

@property
def num_books(self):
    return len(self.books)

The Book class can be implemented as follows:

class Book: def init(self, height, width): self.height = height self.width = width self.is_placed = False self.shelf = None

Testing:

We can test our solution with the following inputs:

bookcases = [Shelf(10, 50), Shelf(10, 50)] books = [Book(6, 40), Book(4, 30), Book(3, 20), Book(5, 10)]

print(fill_bookcase_shelves(bookcases, books)) # expected output: 2

In this case, we have two bookcases, each with a height of 10 and a width of 50. We have four books with different heights and widths. Our function should return 2, indicating that we need two bookcases to store all the books.

Filling Bookcase Shelves Solution Code

1