-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution956.cs
39 lines (33 loc) · 982 Bytes
/
Solution956.cs
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
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution956
{
/// <summary>
/// 956. Tallest Billboard - Hard
/// <a href="https://leetcode.com/problems/tallest-billboard">See the problem</a>
/// </summary>
public int TallestBillboard(int[] rods)
{
int n = rods.Length;
int sum = rods.Sum();
int[] dp = new int[sum + 1];
Array.Fill(dp, -1);
dp[0] = 0;
for (int i = 0; i < n; i++)
{
int[] cur = (int[])dp.Clone();
for (int j = 0; j <= sum - rods[i]; j++)
{
if (dp[j] < 0)
{
continue;
}
cur[j + rods[i]] = Math.Max(cur[j + rods[i]], dp[j]);
cur[Math.Abs(j - rods[i])] = Math.Max(cur[Math.Abs(j - rods[i])], dp[j] + Math.Min(j, rods[i]));
}
dp = cur;
}
return dp[0];
}
}