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]) <= 5000This approach involves using dynamic programming to calculate the maximum possible height of two equal steel supports from given rods. By defining a state that represents the difference of two potential support heights, we can get the maximum height efficiently. The key is to keep track of possible height differences between the two supports as we iterate over each rod.
We use a dictionary dp to store the maximum possible height of the smaller support for each possible height difference. For each rod, we have two choices: add the rod to one support or to the other. We update the dp table accordingly by creating a copy and updating from it to avoid overwriting during iteration.
Java
Time Complexity: O(n * sum), where n is the number of rods and sum is the total length of rods.
Space Complexity: O(sum), where sum is the total length of rods.
This approach involves using dynamic programming with a difference array to explore the possible configurations of rods. We maintain a difference map where the key is the difference in height between two groups, and the value is the maximum sum achievable for that difference.
The idea here is to update the difference map as we iterate through each rod, considering it can contribute to either group or be omitted.
A dictionary dp is maintained where each key denotes a possible difference between the height of two supports, and the value denotes the maximum height obtained for this difference. The new height differences are updated for each rod considered, creating new states for the DP table.
C++
Java
JavaScript
C#
Time Complexity: O(N * S)
Space Complexity: O(S)
where N is the number of rods and S is the sum of all rods.
This approach uses recursive backtracking along with memoization to explore all possible combinations of placing rods into two groups while aiming to minimize the difference between their sum. An auxiliary function is used to recursively try each rod in either of the sets or ignoring it, memoizing results to prevent redundant calculations.
The recursive function dfs explores all possible assignments of rods into the two groups, with memoization applied using lru_cache to store intermediate result computations. This function seeks to balance the groups while maximizing the sum.
C++
Java
JavaScript
C#
Time Complexity: O(N * S)
Space Complexity: O(N * S) for the memoization cache.
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n * sum), where n is the number of rods and sum is the total length of rods. |
| Approach 1: Dynamic Programming using Difference Array | Time Complexity: O(N * S) where N is the number of rods and S is the sum of all rods. |
| Approach 2: Recursive Backtracking with Memoization | Time Complexity: O(N * S) |
Tallest Billboard II DP II Reducing DP States II Hashing II 0/1 Knapsack II Leetcode 956 • Aryan Mittal • 7,654 views views
Watch 9 more video solutions →Practice Tallest Billboard with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor