2
def minHeightShelves(self, books, shelf_width: int) -> int:3
n, dp = len(books), [float("inf")] * (len(books) + 1)6
for i in range(1, n + 1):7
max_width, max_height, j = shelf_width, 0, i - 19
while j >= 0 and max_width - books[j][0] >= 0:10
max_width -= books[j][0]11
max_height = max(max_height, books[j][1])15
if j >= 0 and max_width - books[j][0] < 0:19
while j >= 0 and width + books[j][0] <= shelf_width:20
width = width + books[j][0]21
height = max(books[j][1], height)22
dp[i] = min(dp[i], height + dp[j])