1
var sellingWood = function (m, n, prices) {
2
let price = Array(n + 1)
3
.fill(0)
4
.map(() => Array(m + 1).fill(0));
5
for (let [height, width, woodPrice] of prices) {
6
price[width][height] = woodPrice;
7
}
8
let memo = Array(n + 1)
9
.fill(0)
10
.map(() => Array(m + 1).fill(-1));
11
return dfs(n, m);
12

13
function dfs(width, height) {
14
if (width === 0 || height === 0) return 0;
15
if (memo[width][height] !== -1) return memo[width][height];
16

17
let ans = price[width][height];
18
for (let h = 1; h <= Math.floor(height / 2); h++) {
19
ans = Math.max(ans, dfs(width, h) + dfs(width, height - h));
20
}
21
for (let w = 1; w <= Math.floor(width / 2); w++) {
22
ans = Math.max(ans, dfs(w, height) + dfs(width - w, height));
23
}
24
return (memo[width][height] = ans);
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0