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 <= 201 <= rods[i] <= 1000sum(rods[i]) <= 5000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The tallest billboard problem asks us to find the maximum height of two billboards we can create using a given set of rods. The brute force solution involves trying every possible combination of rods to build the two billboards, exploring all possible arrangements.
Here's how the algorithm would work step-by-step:
def tallest_billboard_brute_force(rods):
maximum_height = 0
def find_max_height(index, sum_billboard_one, sum_billboard_two):
nonlocal maximum_height
# Base case: If we've considered all rods
if index == len(rods):
# If the billboard heights are equal, update the maximum height
if sum_billboard_one == sum_billboard_two:
maximum_height = max(maximum_height, sum_billboard_one)
return
# Recursive step: Try all three choices for the current rod
# Choice 1: Add the rod to the first billboard
find_max_height(index + 1, sum_billboard_one + rods[index], sum_billboard_two)
# Choice 2: Add the rod to the second billboard
find_max_height(index + 1, sum_billboard_one, sum_billboard_two + rods[index])
# Choice 3: Discard the rod
find_max_height(index + 1, sum_billboard_one, sum_billboard_two)
#Initiate the recursive function to explore all possible combinations.
find_max_height(0, 0, 0)
return maximum_heightThe core idea is to think about the problem dynamically. Instead of trying every possible combination of bars, we'll build the billboard height step-by-step, always keeping track of the possible height differences we can achieve between the two supporting structures.
Here's how the algorithm would work step-by-step:
def tallest_billboard(rods):
possible_heights = {0: 0}
for rod_length in rods:
new_heights = possible_heights.copy()
for height_difference, combined_height in possible_heights.items():
#Consider adding to the left support.
new_difference_left = height_difference + rod_length
new_heights[new_difference_left] = max(new_heights.get(new_difference_left, 0), combined_height)
#Consider adding to the right support.
new_difference_right = abs(height_difference - rod_length)
new_height_right = combined_height + min(height_difference, rod_length)
new_heights[new_difference_right] = max(new_heights.get(new_difference_right, 0), new_height_right)
possible_heights = new_heights
# We want the maximum height when the difference is zero.
return possible_heights.get(0, 0)| Case | How to Handle |
|---|---|
| Empty input array | Return 0, as no billboard can be built with no rods. |
| Array with only one rod | Return 0, as one rod cannot form two equal-height towers. |
| Array with all rods of length 0 | Return 0, as the maximum height is 0 when all rods have length 0. |
| Array with large rod lengths causing integer overflow during summation | Use long data type to prevent integer overflow when calculating sums or differences of rod lengths. |
| All rods have the same length | If the number of rods is even, the billboard height is sum/2; otherwise, it's impossible and the height is still 0 using dynamic programming approach. |
| No possible equal-height towers can be built. | The dynamic programming approach correctly returns 0 when no combination results in equal tower heights. |
| Maximum array size to consider time complexity. | Dynamic programming is suitable up to a certain size but a heuristic approach may be needed for larger arrays. |
| Negative Rod Lengths. | Throw an error, as rod lengths cannot be negative. |