# 2312. Selling Pieces of Wood

Contents

## 题目

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 the maximum money you can earn after cutting an `m x n` piece of wood.

Note that you can cut the piece of wood as many times as you want.

Example 1:

```Input: m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
Output: 19
Explanation: 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 19 is the maximum amount of money that can be earned.
```

Example 2:

```Input: m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
Output: 32
Explanation: 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 32 is 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.```

Constraints:

• `1 <= m, n <= 200`
• `1 <= prices.length <= 2 * 104`
• `prices[i].length == 3`
• `1 <= hi <= m`
• `1 <= wi <= n`
• `1 <= pricei <= 106`
• All the shapes of wood `(hi, wi)` are pairwise distinct.

## 一些失败的尝试

### v1.backtracing + memorization，每次取prices中的形状，尝试横切和竖切。

``````import collections
class Solution(object):
def sellingWood(self, m, n, prices):

m1 = collections.defaultdict(list)
for x, y, p in prices:
m1[x].append((y, p))
xs = sorted(m1.keys())
for x in m1:
m1[x] = sorted(m1[x], key=lambda tp:tp[0])

_m = {}
def f(m, n):
if m == 0 or n == 0:
return 0
elif (m, n) in _m:
return _m[(m, n)]
ret = 0
for x, y, p in prices:
# 横切
if x <= m and y <= n:
cur_ans = p + f(x, n - y) + f(m - x, n)
ret = max(cur_ans, ret)
# 竖切
cur_ans = p + f(m - x, y) + f(m, n - y)
ret = max(cur_ans, ret)
_m[(m, n)] = ret
return ret
return f(m, n)``````

### v2.backtracing + memorization，每次取prices中的x值和y值，尝试横切和竖切。

``````import collections
class Solution(object):
def sellingWood(self, m, n, prices):

xs = sorted(set([tp[0] for tp in prices]))
ys = sorted(set([tp[1] for tp in prices]))
mp = {(x, y): p for x, y, p in prices}

_m = {}
def dp(m, n):
if m == 0 or n == 0:
return 0
elif (m, n) in _m:
return _m[(m, n)]
ret = mp[(m, n)] if (m, n) in mp else 0
for x in xs:
if x >= m:
break
ret = max(ret, dp(x, n) + dp(m - x, n))

for y in ys:
if y >= n:
break
ret = max(ret, dp(m, y) + dp(m, n - y))

_m[(m, n)] = ret
return ret
return dp(m, n)``````

## 最终版本

backtracing版本如下。

``````class Solution5(object):
def sellingWood(self, m, n, prices):

mp = {(x, y): p for x, y, p in prices}

_m = {}
def dp(m, n):
if m == 0 or n == 0:
return 0
elif (m, n) in _m:
return _m[(m, n)]
ret = mp[(m, n)] if (m, n) in mp else 0
for x in range(1, m // 2 + 1):
ret = max(ret, dp(x, n) + dp(m - x, n))

for y in range(1, n // 2 + 1):
ret = max(ret, dp(m, y) + dp(m, n - y))

_m[(m, n)] = ret
return ret
return dp(m, n)``````

``````class Solution(object):
def sellingWood(self, m, n, prices):

dp = [[0] * (n + 1) for _ in range(m + 1)]
for x, y, p in prices:
dp[x][y] = p

for i in range(1, m + 1):
for j in range(1, n + 1):
val = 0
for k in range(1, i // 2 + 1):
val = max(val, dp[k][j] + dp[i - k][j])
for k in range(1, j // 2 + 1):
val = max(val, dp[i][k] + dp[i][j - k])
dp[i][j] = max(val, dp[i][j])

return dp[-1][-1]``````

——此处是内容的分割线——