101 Logo
onenoughtone

Code Implementation

Tallest Billboard Implementation

Below is the implementation of the tallest billboard:

solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Find the tallest billboard installation.
* Dynamic Programming with Difference State approach.
*
* @param {number[]} rods - Array of rod lengths
* @return {number} - The largest possible height of the billboard
*/
function tallestBillboard(rods) {
// Initialize dp map
const dp = new Map();
dp.set(0, 0); // With no rods used, both supports have height 0
for (const rod of rods) {
// Create a copy of the current dp map
const currentDp = new Map(dp);
for (const [diff, height] of currentDp) {
// Option 1: Add rod to the taller support
if (diff >= 0) {
dp.set(diff + rod, Math.max(dp.get(diff + rod) || 0, height));
} else {
if (diff + rod >= 0) {
dp.set(diff + rod, Math.max(dp.get(diff + rod) || 0, height + rod + diff));
} else {
dp.set(diff + rod, Math.max(dp.get(diff + rod) || 0, height));
}
}
// Option 2: Add rod to the shorter support
if (diff >= 0) {
if (diff - rod >= 0) {
dp.set(diff - rod, Math.max(dp.get(diff - rod) || 0, height + rod));
} else {
dp.set(rod - diff, Math.max(dp.get(rod - diff) || 0, height + diff));
}
} else {
dp.set(diff - rod, Math.max(dp.get(diff - rod) || 0, height));
}
}
}
return dp.get(0) || 0; // Return the maximum height when both supports have the same height
}
/**
* Find the tallest billboard installation.
* Simplified Dynamic Programming approach.
*
* @param {number[]} rods - Array of rod lengths
* @return {number} - The largest possible height of the billboard
*/
function tallestBillboardSimplified(rods) {
// Initialize dp map
const dp = new Map();
dp.set(0, 0); // With no rods used, both supports have height 0
for (const rod of rods) {
// Create a copy of the current dp map
const currentDp = new Map(dp);
for (const [diff, height] of currentDp) {
// Option 1: Add rod to the first support
dp.set(diff + rod, Math.max(dp.get(diff + rod) || 0, height + rod));
// Option 2: Add rod to the second support
dp.set(diff - rod, Math.max(dp.get(diff - rod) || 0, height));
}
}
return dp.get(0) || 0; // Return the maximum height when both supports have the same height
}
// Test cases
console.log(tallestBillboard([1, 2, 3, 6])); // 6
console.log(tallestBillboard([1, 2, 3, 4, 5, 6])); // 10
console.log(tallestBillboard([1, 2])); // 0
console.log(tallestBillboardSimplified([1, 2, 3, 6])); // 6
console.log(tallestBillboardSimplified([1, 2, 3, 4, 5, 6])); // 10
console.log(tallestBillboardSimplified([1, 2])); // 0

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that we need to find the maximum height of a billboard with two equal-height supports, using a collection of rods.
  2. Define the State: Define the state dp[diff] as the maximum height of the shorter support when the difference between the heights of the two supports is diff.
  3. Consider All Options: For each rod, consider three options: add it to the taller support, add it to the shorter support, or don't use it.
  4. Handle Edge Cases: Handle edge cases such as when the difference becomes negative or when a support becomes taller than the other after adding a rod.
  5. Return the Result: Return dp[0], which represents the maximum height when both supports have the same height.
ProblemSolutionCode
101 Logo
onenoughtone