> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Tallest Billboard Python Solution with Tests

> Tested Python solution for LeetCode 956 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 956, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), Meet in the Middle, Knapsack Problem, 0-1 Knapsack. [View on LeetCode](https://leetcode.com/problems/tallest-billboard/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 956   # by problem number
lcpy gen -s tallest_billboard   # by problem name
```

## Problem

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`.

### Examples

```
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.

```
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.

```
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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/tallest_billboard/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/tallest_billboard/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * diff) where diff <= sum(rods)
    # Space: O(diff)
    def tallest_billboard(self, rods: list[int]) -> int:
        # dp[d] = largest total height of the taller support when the
        # two supports differ by exactly d (d >= 0).
        dp: dict[int, int] = {0: 0}
        for rod in rods:
            nxt = dict(dp)
            for diff, taller in dp.items():
                # put the rod on the taller support
                nxt[diff + rod] = max(nxt.get(diff + rod, 0), taller + rod)
                # put the rod on the shorter support
                if rod >= diff:
                    nxt[rod - diff] = max(nxt.get(rod - diff, 0), taller - diff + rod)
                else:
                    nxt[diff - rod] = max(nxt.get(diff - rod, 0), taller)
            dp = nxt
        return dp.get(0, 0)
```

## Complexity

| Time | Space |
| - | - |
| O(n \* diff) where diff \<= sum(rods) | O(diff) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.