Skip to content

Commit 75ca64d

Browse files
authored
Create tallest-billboard.py
1 parent b5db135 commit 75ca64d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/tallest-billboard.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n * 3^(n/2))
2+
# Space: O(3^(n/2))
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def tallestBillboard(self, rods):
9+
"""
10+
:type rods: List[int]
11+
:rtype: int
12+
"""
13+
def dp(A):
14+
lookup = collections.defaultdict(int)
15+
lookup[0] = 0
16+
for x in A:
17+
for d, y in lookup.items():
18+
lookup[d+x] = max(lookup[d+x], y)
19+
lookup[abs(d-x)] = max(lookup[abs(d-x)], y + min(d, x))
20+
return lookup
21+
22+
left, right = dp(rods[:len(rods)//2]), dp(rods[len(rods)//2:])
23+
return max(left[d]+right[d]+d for d in left if d in right)

0 commit comments

Comments
 (0)