Skip to content

Commit 484cb99

Browse files
committed
Add solution #956
1 parent 8592991 commit 484cb99

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,041 LeetCode solutions in JavaScript
1+
# 1,042 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -765,6 +765,7 @@
765765
953|[Verifying an Alien Dictionary](./solutions/0953-verifying-an-alien-dictionary.js)|Easy|
766766
954|[Array of Doubled Pairs](./solutions/0954-array-of-doubled-pairs.js)|Medium|
767767
955|[Delete Columns to Make Sorted II](./solutions/0955-delete-columns-to-make-sorted-ii.js)|Medium|
768+
956|[Tallest Billboard](./solutions/0956-tallest-billboard.js)|Hard|
768769
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
769770
970|[Powerful Integers](./solutions/0970-powerful-integers.js)|Easy|
770771
976|[Largest Perimeter Triangle](./solutions/0976-largest-perimeter-triangle.js)|Easy|

solutions/0956-tallest-billboard.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)