-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1130.cs
44 lines (37 loc) · 1.09 KB
/
Solution1130.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
40
41
42
43
44
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1130
{
/// <summary>
/// 1130. Minimum Cost Tree From Leaf Values - Medium
/// <a href="https://leetcode.com/problems/minimum-cost-tree-from-leaf-values">See the problem</a>
/// </summary>
public int MctFromLeafValues(int[] arr)
{
var n = arr.Length;
var dp = new int[n, n];
var max = new int[n, n];
for (var i = 0; i < n; i++)
{
max[i, i] = arr[i];
for (var j = i + 1; j < n; j++)
{
max[i, j] = Math.Max(max[i, j - 1], arr[j]);
}
}
for (var len = 2; len <= n; len++)
{
for (var i = 0; i <= n - len; i++)
{
var j = i + len - 1;
dp[i, j] = int.MaxValue;
for (var k = i; k < j; k++)
{
dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k + 1, j] + max[i, k] * max[k + 1, j]);
}
}
}
return dp[0, n - 1];
}
}