-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1105.py
27 lines (23 loc) · 1.07 KB
/
m1105.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
class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
@cache
def recurs(currBook: int = 0,
height: int = 0,
widthRemaining: int = 0) -> int :
if currBook >= len(books) :
return 0
# New row
output = books[currBook][1] + recurs(currBook + 1,
books[currBook][1],
shelfWidth - books[currBook][0])
# Can current book can fit in current row
if widthRemaining - books[currBook][0] >= 0 :
heightIncr = max(0, books[currBook][1] - height)
output = min(output,
heightIncr + recurs(currBook + 1,
height + heightIncr,
widthRemaining - books[currBook][0]))
return output
if not books :
return 0
return recurs()