Skip to content

Commit 0bf4c53

Browse files
committed
Time: 45 ms (88.70%), Space: 17 MB (59.89%) - LeetHub
1 parent 4302450 commit 0bf4c53

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
3+
# dp[i] will store the minimum height for the first i books
4+
dp = [0] * (len(books) + 1)
5+
6+
# Start filling the dp array
7+
for i in range(1, len(books) + 1):
8+
total_width = 0
9+
max_height = 0
10+
dp[i] = float('inf')
11+
# Place books[i-1] on the current shelf
12+
for j in range(i, 0, -1):
13+
total_width += books[j-1][0]
14+
if total_width > shelfWidth:
15+
break
16+
max_height = max(max_height, books[j-1][1])
17+
dp[i] = min(dp[i], dp[j-1] + max_height)
18+
19+
return dp[len(books)]

0 commit comments

Comments
 (0)