Tallest Billboard Problem
Description
LeetCode Problem 956.
You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.
You are given a collection of rods that can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.
Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.
Example 1:
Input: rods = [1,2,3,6]
Output: 6
Explanation: We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.
Example 2:
Input: rods = [1,2,3,4,5,6]
Output: 10
Explanation: We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.
Example 3:
Input: rods = [1,2]
Output: 0
Explanation: The billboard cannot be supported, so we return 0.
Constraints:
- 1 <= rods.length <= 20
- 1 <= rods[i] <= 1000
- sum(rods[i]) <= 5000
Solution
Approach: Dynamic Programming on the Height Difference
Each rod goes to the left support, to the right support, or is not used, so trying every option takes O(3n) time. The key observation is that, while we build the two supports, only their difference matters for the future: two partial builds with the same difference can be finished with exactly the same remaining rods. Among them, we only need the one whose supports are tallest.
So let best[d] be the tallest possible shorter support over all ways to use the rods seen so far that leave the taller support exactly d higher (-1 if no way gives d). Start with best[0] = 0. For each rod r and each reachable d with shorter height s = best[d]:
- Skip the rod: the state stays
best[d] = s. - Add it to the taller support: the gap becomes
d + rand the shorter side stayss. - Add it to the shorter support: the gap becomes
|d - r|. The new shorter side iss + min(d, r): ifr <= dthe shorter support grows byr, and ifr > dit overtakes the other one, so the old taller support (heights + d) becomes the shorter.
Build the new table from the old one for each rod (a copy), so that one rod is never used twice. The answer is best[0]: equal supports, as tall as possible. It is 0 when only the empty build has difference 0.
The gap can never exceed S, the sum of all rods, so the table has S + 1 entries.
Complexity: O(n · S) time and O(S) space, where n is the number of rods and S their total length.
class Solution:
def tallestBillboard(self, rods: List[int]) -> int:
total = sum(rods)
# best[d] = tallest possible shorter support when the taller one is d higher; -1 = unreachable.
best = [-1] * (total + 1)
best[0] = 0
for rod in rods:
updated = best[:] # leaving the rod out keeps every old state
for diff in range(total + 1):
if best[diff] < 0:
continue
shorter = best[diff]
# Weld the rod onto the taller support: the gap grows.
if diff + rod <= total:
updated[diff + rod] = max(updated[diff + rod], shorter)
# Weld it onto the shorter support: the gap becomes |diff - rod|,
# and the new shorter side grows by min(diff, rod).
new_diff = abs(diff - rod)
updated[new_diff] = max(updated[new_diff], shorter + min(diff, rod))
best = updated
return best[0]
Now write it from memory
Close the write-up and solve it yourself against the tests.