-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
59 lines (50 loc) · 1.6 KB
/
Copy pathsolution.cpp
File metadata and controls
59 lines (50 loc) · 1.6 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution
{
public:
int minCost(vector<int> &keys, vector<int> &freq)
{
int n = keys.size();
if (n == 0)
return 0;
// dp[i][j] = minimum cost of optimal BST using keys[i..j]
vector<vector<int>> dp(n, vector<int>(n, 0));
// prefixFreq[k] = sum of freq[0..k-1]
vector<int> prefixFreq(n + 1, 0);
for (int i = 0; i < n; ++i)
{
prefixFreq[i + 1] = prefixFreq[i] + freq[i];
}
// Helper lambda to get sum of freq from i to j inclusive
auto rangeSum = [&](int i, int j)
{
return prefixFreq[j + 1] - prefixFreq[i];
};
// Base case: single key
for (int i = 0; i < n; ++i)
{
dp[i][i] = freq[i];
}
// length from 2 to n
for (int len = 2; len <= n; ++len)
{
for (int i = 0; i + len - 1 < n; ++i)
{
int j = i + len - 1;
dp[i][j] = INT_MAX;
int sumFreq = rangeSum(i, j); // constant extra cost for this segment
// Try every key as root
for (int r = i; r <= j; ++r)
{
int left = (r > i) ? dp[i][r - 1] : 0;
int right = (r < j) ? dp[r + 1][j] : 0;
int cost = left + right + sumFreq;
if (cost < dp[i][j])
{
dp[i][j] = cost;
}
}
}
}
return dp[0][n - 1];
}
};