Skip to content

Commit 279e3d8

Browse files
authored
Create filling-bookcase-shelves.py
1 parent fad5e45 commit 279e3d8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python/filling-bookcase-shelves.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(n^2)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def minHeightShelves(self, books, shelf_width):
6+
"""
7+
:type books: List[List[int]]
8+
:type shelf_width: int
9+
:rtype: int
10+
"""
11+
dp = [float("inf") for _ in xrange(len(books)+1)]
12+
dp[0] = 0
13+
for i in xrange(1, len(books)+1):
14+
max_width = shelf_width
15+
max_height = 0
16+
for j in reversed(xrange(i)):
17+
if max_width-books[j][0] < 0:
18+
break
19+
max_width -= books[j][0]
20+
max_height = max(max_height, books[j][1])
21+
dp[i] = min(dp[i], dp[j]+max_height)
22+
return dp[len(books)]

0 commit comments

Comments
 (0)