Tallest Billboard

hard dynamic programming subset sum array

Problem

You are installing a billboard supported by two steel rods of equal height. You have a collection of rods (lengths in rods) and may weld several together to form a support. Each rod is used in the left support, the right support, or not at all. Return the largest possible height of the billboard (the common height of the two supports), or 0 if you cannot make two equal supports.

Inputrods = [1, 2, 3, 6]
Output6
Use {1, 2, 3} on one side and {6} on the other — both supports are height 6.

def tallest_billboard(rods):
    # dp[diff] = max height of taller support for that difference
    dp = {0: 0}
    for r in rods:
        cur = dict(dp)
        for diff, taller in cur.items():
            shorter = taller - diff
            # add r to taller support
            d = diff + r
            dp[d] = max(dp.get(d, 0), taller + r)
            # add r to shorter support
            d = abs(shorter + r - taller)
            dp[d] = max(dp.get(d, 0), max(taller, shorter + r))
    return dp[0]
function tallestBillboard(rods) {
  let dp = { 0: 0 };
  for (const r of rods) {
    const cur = Object.assign({}, dp);
    for (const key in cur) {
      const diff = Number(key), taller = cur[key];
      const shorter = taller - diff;
      let d = diff + r;
      dp[d] = Math.max(dp[d] || 0, taller + r);
      d = Math.abs(shorter + r - taller);
      dp[d] = Math.max(dp[d] || 0, Math.max(taller, shorter + r));
    }
  }
  return dp[0];
}
class Solution {
    public int tallestBillboard(int[] rods) {
        Map<Integer, Integer> dp = new HashMap<>();
        dp.put(0, 0);
        for (int r : rods) {
            Map<Integer, Integer> cur = new HashMap<>(dp);
            for (Map.Entry<Integer, Integer> e : cur.entrySet()) {
                int diff = e.getKey(), taller = e.getValue();
                int shorter = taller - diff;
                int d = diff + r;
                dp.put(d, Math.max(dp.getOrDefault(d, 0), taller + r));
                d = Math.abs(shorter + r - taller);
                dp.put(d, Math.max(dp.getOrDefault(d, 0), Math.max(taller, shorter + r)));
            }
        }
        return dp.get(0);
    }
}
int tallestBillboard(vector<int>& rods) {
    unordered_map<int, int> dp;
    dp[0] = 0;
    for (int r : rods) {
        unordered_map<int, int> cur = dp;
        for (auto& e : cur) {
            int diff = e.first, taller = e.second;
            int shorter = taller - diff;
            int d = diff + r;
            dp[d] = max(dp.count(d) ? dp[d] : 0, taller + r);
            d = abs(shorter + r - taller);
            dp[d] = max(dp.count(d) ? dp[d] : 0, max(taller, shorter + r));
        }
    }
    return dp[0];
}
Time: O(n · S) Space: O(S)