-
Notifications
You must be signed in to change notification settings - Fork 17
/
filling-bookcase-shelves.py
50 lines (39 loc) · 1.68 KB
/
filling-bookcase-shelves.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import List
from functools import lru_cache
class Solution:
def minHeightShelves(self, books: List[List[int]], shelf_width: int) -> int:
dp = [float("+inf")] * (len(books) + 1)
dp[0] = 0
for start in range(len(books)):
level_width = 0
level_height = 0
end = start
while level_width <= shelf_width and end <= len(books):
dp[end] = min(dp[end], dp[start] + level_height,)
if end < len(books):
width, height = books[end]
level_width += width
level_height = max(level_height, height)
end += 1
return dp[-1]
def minHeightShelvesTopDown(self, books: List[List[int]], shelf_width: int) -> int:
@lru_cache(None)
def dp(book: int, level_height: int, level_width: int) -> int:
if book == len(books):
return level_height
width, height = books[book]
total_height = float("+inf")
if level_width + width < shelf_width:
total_height = min(
dp(book + 1, max(level_height, height), level_width + width),
dp(book + 1, 0, 0) + max(level_height, height),
total_height,
)
elif level_width + width == shelf_width:
total_height = min(
dp(book + 1, 0, 0) + max(level_height, height), total_height,
)
elif level_width + width > shelf_width:
total_height = min(dp(book, 0, 0) + level_height, total_height,)
return total_height
return dp(0, 0, 0)