Skip to content
Prev Previous commit
Next Next commit
1105
  • Loading branch information
theforestvn88 committed Jul 31, 2024
commit 43564cdfb313b6409171b080a88aa4c3cbbdd453
28 changes: 28 additions & 0 deletions 1101-1200/1105_filling_bookcase_shelves.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# @param {Integer[][]} books
# @param {Integer} shelf_width
# @return {Integer}
def min_height_shelves(books, shelf_width)
# defining dp[i] as the min height for placing i books (IN ORDER)
dp = Array.new(books.size + 1, Float::INFINITY)
dp[0] = 0
(0...books.size).each { |i|
# for the next i-th book
w, h = books[i]
# we can:
# place it on another level
dp[i+1] = dp[i] + h
# collect books [j..i-1] and place them at the same level, 0 <= j < i
(i-1).downto(0) { |j|
w += books[j][0]
# Note that we need to place books IN ORDER
# so we cannot place the i-th book at the same level of books [j..i-2] which above the (i-1)th book
# therefor, if the width of books [j..i] is out of the shelf width, we stop
break if w > shelf_width

h = [h, books[j][1]].max
dp[i+1] = [dp[i+1], dp[j] + h].min
}
}

dp[books.size]
end