You are given two integers m and n that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array prices, where prices[i] = [hi, wi, pricei] indicates you can sell a rectangular piece of wood of height hi and width wi for pricei dollars.
To cut a piece of wood, you must make a vertical or horizontal cut across the
entire height or width of the piece to split it into two smaller pieces.
After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to prices. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you cannot rotate a piece to swap its height and width.
Return _themaximum money you can earn after cutting an _m x npiece of wood.
Note that you can cut the piece of wood as many times as you want.

Input: m =3, n =5, prices =[[1,4,2],[2,2,7],[2,1,3]]Output: 19Explanation: The diagram above shows a possible scenario. It consists of:-2 pieces of wood shaped 2 x 2, selling for a price of 2*7=14.-1 piece of wood shaped 2 x 1, selling for a price of 1*3=3.-1 piece of wood shaped 1 x 4, selling for a price of 1*2=2.This obtains a total of 14+3+2=19 money earned.It can be shown that 19is the maximum amount of money that can be earned.

Input: m =4, n =6, prices =[[3,2,10],[1,4,2],[4,1,3]]Output: 32Explanation: The diagram above shows a possible scenario. It consists of:-3 pieces of wood shaped 3 x 2, selling for a price of 3*10=30.-1 piece of wood shaped 1 x 4, selling for a price of 1*2=2.This obtains a total of 30+2=32 money earned.It can be shown that 32is the maximum amount of money that can be earned.Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.
This problem is a classic 2D DP with memoization. For each subrectangle, we want to know the maximum profit we can get by either selling it directly (if a price is given) or by cutting it into smaller pieces and selling those. We recursively try all possible horizontal and vertical cuts, memoizing results to avoid recomputation.
import java.util.*;
classSolution {
publiclongsellingWood(int m, int n, int[][] prices) {
long[][] price =newlong[m+1][n+1];
for (int[] p : prices) price[p[0]][p[1]]= p[2];
long[][] dp =newlong[m+1][n+1];
for (int i = 0; i <= m; ++i)
Arrays.fill(dp[i], -1);
return dfs(m, n, price, dp);
}
privatelongdfs(int h, int w, long[][] price, long[][] dp) {
if (dp[h][w]!=-1) return dp[h][w];
long res = price[h][w];
for (int i = 1; i < h; ++i)
res = Math.max(res, dfs(i, w, price, dp) + dfs(h-i, w, price, dp));
for (int j = 1; j < w; ++j)
res = Math.max(res, dfs(h, j, price, dp) + dfs(h, w-j, price, dp));
return dp[h][w]= res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funsellingWood(m: Int, n: Int, prices: Array<IntArray>): Long {
val price = Array(m+1) { LongArray(n+1) }
for (p in prices) price[p[0]][p[1]] = p[2].toLong()
val dp = Array(m+1) { LongArray(n+1) { -1L } }
fundfs(h: Int, w: Int): Long {
if (dp[h][w] != -1L) return dp[h][w]
var res = price[h][w]
for (i in1 until h) res = maxOf(res, dfs(i, w) + dfs(h-i, w))
for (j in1 until w) res = maxOf(res, dfs(h, j) + dfs(h, w-j))
dp[h][w] = res
return res
}
return dfs(m, n)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from functools import lru_cache
defsellingWood(m, n, prices):
price = [[0]*(n+1) for _ in range(m+1)]
for h, w, p in prices:
price[h][w] = p
@lru_cache(None)
defdp(h, w):
res = price[h][w]
for i in range(1, h):
res = max(res, dp(i, w) + dp(h-i, w))
for j in range(1, w):
res = max(res, dp(h, j) + dp(h, w-j))
return res
return dp(m, n)