leetcode-956-Tallest Billboard
问题
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 have a collection of rods
which 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.
Example 1:
1 | Input: [1,2,3,6] |
Example 2:
1 | Input: [1,2,3,4,5,6] |
Example 3:
1 | Input: [1,2] |
Note:
0 <= rods.length <= 20
1 <= rods[i] <= 1000
The sum of rods is at most 5000.
分析
$dp[i][j] == h$ 表示使用 $rods[0..i]$ 中的一些珠子来构建两个柱子,高度差为 $j$ 时,最大可能的公共高度为 $h$ 。则当遍历到 $rods[i]$ 时,可以有三种选择,第一种选择是不使用 $rods[i]$,第二种选择是将 $rods[i]$ 加到高一点的柱子上,第三种选择是将 $rods[i]$ 加到低一点的柱子上。将 $rods[i]$ 加到哪个柱子上就取决于 $dp[i-1][k]$。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/04/08/leetcode-956/
License: 知识共享署名-非商业性使用 4.0 国际许可协议