|
| 1 | +/** |
| 2 | + * 956. Tallest Billboard |
| 3 | + * https://leetcode.com/problems/tallest-billboard/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are installing a billboard and want it to have the largest height. The billboard will have |
| 7 | + * two steel supports, one on each side. Each steel support must be an equal height. |
| 8 | + * |
| 9 | + * You are given a collection of rods that can be welded together. For example, if you have rods |
| 10 | + * of lengths 1, 2, and 3, you can weld them together to make a support of length 6. |
| 11 | + * |
| 12 | + * Return the largest possible height of your billboard installation. If you cannot support the |
| 13 | + * billboard, return 0. |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {number[]} rods |
| 18 | + * @return {number} |
| 19 | + */ |
| 20 | +var tallestBillboard = function(rods) { |
| 21 | + const differences = new Map([[0, 0]]); |
| 22 | + |
| 23 | + for (const rod of rods) { |
| 24 | + const currentDiffs = new Map(differences); |
| 25 | + |
| 26 | + for (const [diff, taller] of currentDiffs) { |
| 27 | + const shorter = taller - diff; |
| 28 | + const newDiff = diff + rod; |
| 29 | + const newTaller = taller + rod; |
| 30 | + differences.set(newDiff, Math.max(differences.get(newDiff) || 0, newTaller)); |
| 31 | + |
| 32 | + const diffAbs = Math.abs(diff - rod); |
| 33 | + const diffMax = Math.max(shorter + rod, taller); |
| 34 | + differences.set(diffAbs, Math.max(differences.get(diffAbs) || 0, diffMax)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return differences.get(0) || 0; |
| 39 | +}; |
0 commit comments