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:

1
2
3
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:

1
2
3
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:

1
2
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


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
    int tallestBillboard(vector<int>& nums) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        vector<int> dp(sum + 1, -1), curr;
        dp[0] = 0;
        for (int &el : nums) {
            // storing the immediate calculated values
            curr = dp;
            for (int i = 0; i <= sum - el; ++i) {
                // not computed still now, so skip it
                if (curr[i] < 0)
                    continue;
                // putting rod into heighest side
                dp[i + el] = max(dp[i + el], curr[i]);
                // putting rod into smallest side
                dp[abs(i - el)] = max(dp[abs(i - el)], curr[i] + min(i, el));
            }
        }
        // we need equal sizes so diff =0 will be my ans
        return dp[0];
    }
};




Related Posts

Valid Permutations For DI Sequence Problem

LeetCode 903. You are given a string s of length...

Tallest Billboard Problem

LeetCode 956. You are installing a billboard and want it...

Sum Of Subarray Minimums Problem

LeetCode 907. Given an array of integers arr, find the...

Stone Game Problem

LeetCode 877. Alice and Bob play a game with piles...

Split Array With Same Average Problem

LeetCode 805. You are given an integer array nums.

Soup Servings Problem

LeetCode 808. There are two types of soup, type A...