Filling Bookcase shelves solution

from typing import List
class Solution:
    def minHeightShelves(self, b: List[List[int]], w: int) -> int:
        print(f'Books: {b}\nShelf width: {w}')
        n = len(b)
        dp = [float('inf')] * (n + 1)
        dp[0] = 0
        print(f'initial dp: {dp}')
        for i in range(n):
            print()
            print('-' * 80)
            print(f'[{i+1} books] to consider: {b[:i+1]}')
            j = i
            width = 0
            h = 0
            while j >= 0:
                print(f'place [{j}]-th book: {b[j]}')
                width += b[j][0]
                if width > w:
                    print('The total width is beyond the shelf')
                    break
                print(f'The maximum height is updated from [{h}] to [{max(h, b[j][1])}]')
                h = max(h, b[j][1])
                
                print(f'The minimum total height for [{i+1} books] is update from [{dp[i + 1]}] to [{min(dp[i + 1], dp[j] + h)}]')
                dp[i + 1] = min(dp[i + 1], dp[j] + h)
                j -= 1
        return dp[-1]     

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source