Skip to content
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

  1. Initialization: Initialize a dp array where dp[0] = 0 (no books, no height).
  2. Dynamic Programming Transition: For each book i, try to place it on the current shelf or start a new shelf. Update dp[i] with the minimum possible height after placing book i.
  3. Iterate through books: For each book, check all possible ways to place it on the current shelf considering the shelfWidth. Update the dp array accordingly.

Let's implement this solution in PHP: 1105. Filling Bookcase Shelves

<?php

function minHeightShelves($books, $shelfWidth) {
    $n = count($books);
    $dp = array_fill(0, $n + 1, PHP_INT_MAX);
    $dp[0] = 0;
    
    for ($i = 1; $i <= 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
1 participant