Skip to content

Commit bbbf038

Browse files
committed
Runtime: 193 ms (Top 7.04%) | Memory: 48.6 MB (Top 15.49%)
1 parent 3dba59a commit bbbf038

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1+
// Runtime: 193 ms (Top 7.04%) | Memory: 48.6 MB (Top 15.49%)
12
var mctFromLeafValues = function(arr) {
23
const dp = [];
34

45
for (let i = 0; i < arr.length; i++) {
56
dp[i] = [];
67
}
7-
8+
89
return treeBuilder(0, arr.length - 1);
9-
10+
1011
function treeBuilder(start, end) {
11-
12+
1213
if (start == end) {
1314
return 0;
1415
}
15-
16+
1617
if (dp[start][end]) {
1718
return dp[start][end];
1819
}
19-
20+
2021
let min = Number.MAX_VALUE;
21-
22+
2223
for (let i = start; i < end; i++) {
2324
const left = treeBuilder(start, i);
2425
const right = treeBuilder(i + 1, end);
25-
26+
2627
const maxLeft = Math.max(...arr.slice(start, i + 1));
2728
const maxRight = Math.max(...arr.slice(i + 1, end + 1));
28-
29+
2930
const rootVal = maxLeft * maxRight;
30-
31+
3132
min = Math.min(min, rootVal + left + right);
3233

3334
}
34-
35+
3536
dp[start][end] = min;
3637
return min;
3738
}
38-
};
39+
};

0 commit comments

Comments
 (0)