Skip to content

Commit eee905d

Browse files
committed
+ problem 2312
1 parent ccaa33f commit eee905d

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 2312. Selling Pieces of Wood
2+
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 <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> indicates you can sell a rectangular piece of wood of height <code>h<sub>i</sub></code> and width <code>w<sub>i</sub></code> for <code>price<sub>i</sub></code> dollars.
3+
4+
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.
5+
6+
Return *the **maximum** money you can earn after cutting an* `m x n` *piece of wood*.
7+
8+
Note that you can cut the piece of wood as many times as you want.
9+
10+
#### Example 1:
11+
![](https://assets.leetcode.com/uploads/2022/04/27/ex1.png)
12+
<pre>
13+
<strong>Input:</strong> m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
14+
<strong>Output:</strong> 19
15+
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
16+
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
17+
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
18+
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
19+
This obtains a total of 14 + 3 + 2 = 19 money earned.
20+
It can be shown that 19 is the maximum amount of money that can be earned.
21+
</pre>
22+
23+
#### Example 2:
24+
![](https://assets.leetcode.com/uploads/2022/04/27/ex2new.png)
25+
<pre>
26+
<strong>Input:</strong> m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
27+
<strong>Output:</strong> 32
28+
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
29+
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 = 30.
30+
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
31+
This obtains a total of 30 + 2 = 32 money earned.
32+
It can be shown that 32 is the maximum amount of money that can be earned.
33+
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.
34+
</pre>
35+
36+
#### Constraints:
37+
* `1 <= m, n <= 200`
38+
* <code>1 <= prices.length <= 2 * 10<sup>4</sup></code>
39+
* `prices[i].length == 3`
40+
* <code>1 <= h<sub>i</sub> <= m</code>
41+
* <code>1 <= w<sub>i</sub> <= n</code>
42+
* <code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code>
43+
* All the shapes of wood <code>(h<sub>i</sub>, w<sub>i</sub>)</code> are pairwise **distinct**.
44+
45+
## Solutions (Python)
46+
47+
### 1. Solution
48+
```Python
49+
from functools import cache
50+
51+
52+
class Solution:
53+
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
54+
prices = {(h, w): price for h, w, price in prices}
55+
56+
@cache
57+
def maxSelling(m: int, n: int) -> int:
58+
ret = prices.get((m, n), 0)
59+
60+
for h in range(1, m):
61+
ret = max(ret, maxSelling(h, n) + maxSelling(m - h, n))
62+
for w in range(1, n):
63+
ret = max(ret, maxSelling(m, w) + maxSelling(m, n - w))
64+
65+
return ret
66+
67+
return maxSelling(m, n)
68+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 2312. 卖木头块
2+
给你两个整数 `m``n` ,分别表示一块矩形木块的高和宽。同时给你一个二维整数数组 `prices` ,其中 <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> 表示你可以以 <code>price<sub>i</sub></code> 元的价格卖一块高为 <code>h<sub>i</sub></code> 宽为 <code>w<sub>i</sub></code> 的矩形木块。
3+
4+
每一次操作中,你必须按下述方式之一执行切割操作,以得到两块更小的矩形木块:
5+
6+
* 沿垂直方向按高度 **完全** 切割木块,或
7+
* 沿水平方向按宽度 **完全** 切割木块
8+
9+
在将一块木块切成若干小木块后,你可以根据 `prices` 卖木块。你可以卖多块同样尺寸的木块。你不需要将所有小木块都卖出去。你 **不能** 旋转切好后木块来交换它的高度值和宽度值。
10+
11+
请你返回切割一块大小为 `m x n` 的木块后,能得到的 **最多** 钱数。
12+
13+
注意你可以切割木块任意次。
14+
15+
#### 示例 1:
16+
![](https://assets.leetcode.com/uploads/2022/04/27/ex1.png)
17+
<pre>
18+
<strong>输入:</strong> m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
19+
<strong>输出:</strong> 19
20+
<strong>解释:</strong> 上图展示了一个可行的方案。包括:
21+
- 2 块 2 x 2 的小木块,售出 2 * 7 = 14 元。
22+
- 1 块 2 x 1 的小木块,售出 1 * 3 = 3 元。
23+
- 1 块 1 x 4 的小木块,售出 1 * 2 = 2 元。
24+
总共售出 14 + 3 + 2 = 19 元。
25+
19 元是最多能得到的钱数。
26+
</pre>
27+
28+
#### 示例 2:
29+
![](https://assets.leetcode.com/uploads/2022/04/27/ex2new.png)
30+
<pre>
31+
<strong>输入:</strong> m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
32+
<strong>输出:</strong> 32
33+
<strong>解释:</strong> 上图展示了一个可行的方案。包括:
34+
- 3 块 3 x 2 的小木块,售出 3 * 10 = 30 元。
35+
- 1 块 1 x 4 的小木块,售出 1 * 2 = 2 元。
36+
总共售出 30 + 2 = 32 元。
37+
32 元是最多能得到的钱数。
38+
注意我们不能旋转 1 x 4 的木块来得到 4 x 1 的木块。
39+
</pre>
40+
41+
#### 提示:
42+
* `1 <= m, n <= 200`
43+
* <code>1 <= prices.length <= 2 * 10<sup>4</sup></code>
44+
* `prices[i].length == 3`
45+
* <code>1 <= h<sub>i</sub> <= m</code>
46+
* <code>1 <= w<sub>i</sub> <= n</code>
47+
* <code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code>
48+
* 所有 <code>(h<sub>i</sub>, w<sub>i</sub>)</code> 互不相同 。
49+
50+
## 题解 (Python)
51+
52+
### 1. 题解
53+
```Python
54+
from functools import cache
55+
56+
57+
class Solution:
58+
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
59+
prices = {(h, w): price for h, w, price in prices}
60+
61+
@cache
62+
def maxSelling(m: int, n: int) -> int:
63+
ret = prices.get((m, n), 0)
64+
65+
for h in range(1, m):
66+
ret = max(ret, maxSelling(h, n) + maxSelling(m - h, n))
67+
for w in range(1, n):
68+
ret = max(ret, maxSelling(m, w) + maxSelling(m, n - w))
69+
70+
return ret
71+
72+
return maxSelling(m, n)
73+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
6+
prices = {(h, w): price for h, w, price in prices}
7+
8+
@cache
9+
def maxSelling(m: int, n: int) -> int:
10+
ret = prices.get((m, n), 0)
11+
12+
for h in range(1, m):
13+
ret = max(ret, maxSelling(h, n) + maxSelling(m - h, n))
14+
for w in range(1, n):
15+
ret = max(ret, maxSelling(m, w) + maxSelling(m, n - w))
16+
17+
return ret
18+
19+
return maxSelling(m, n)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,7 @@
13691369
[2306][2306l]|[Naming a Company][2306] |![py]
13701370
[2309][2309l]|[Greatest English Letter in Upper and Lower Case][2309] |![rs]
13711371
[2310][2310l]|[Sum of Numbers With Units Digit K][2310] |![rs]
1372+
[2312][2312l]|[Selling Pieces of Wood][2312] |![py]
13721373
[2315][2315l]|[Count Asterisks][2315] |![rs]
13731374
[2317][2317l]|[Maximum XOR After Operations][2317] |![rs]
13741375
[2319][2319l]|[Check if Matrix Is X-Matrix][2319] |![py]
@@ -2880,6 +2881,7 @@
28802881
[2306]:Problemset/2306-Naming%20a%20Company/README.md#2306-naming-a-company
28812882
[2309]:Problemset/2309-Greatest%20English%20Letter%20in%20Upper%20and%20Lower%20Case/README.md#2309-greatest-english-letter-in-upper-and-lower-case
28822883
[2310]:Problemset/2310-Sum%20of%20Numbers%20With%20Units%20Digit%20K/README.md#2310-sum-of-numbers-with-units-digit-k
2884+
[2312]:Problemset/2312-Selling%20Pieces%20of%20Wood/README.md#2312-selling-pieces-of-wood
28832885
[2315]:Problemset/2315-Count%20Asterisks/README.md#2315-count-asterisks
28842886
[2317]:Problemset/2317-Maximum%20XOR%20After%20Operations/README.md#2317-maximum-xor-after-operations
28852887
[2319]:Problemset/2319-Check%20if%20Matrix%20Is%20X-Matrix/README.md#2319-check-if-matrix-is-x-matrix
@@ -4390,6 +4392,7 @@
43904392
[2306l]:https://leetcode.com/problems/naming-a-company/
43914393
[2309l]:https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/
43924394
[2310l]:https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/
4395+
[2312l]:https://leetcode.com/problems/selling-pieces-of-wood/
43934396
[2315l]:https://leetcode.com/problems/count-asterisks/
43944397
[2317l]:https://leetcode.com/problems/maximum-xor-after-operations/
43954398
[2319l]:https://leetcode.com/problems/check-if-matrix-is-x-matrix/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,7 @@
13691369
[2306][2306l]|[公司命名][2306] |![py]
13701370
[2309][2309l]|[兼具大小写的最好英文字母][2309] |![rs]
13711371
[2310][2310l]|[个位数字为 K 的整数之和][2310] |![rs]
1372+
[2312][2312l]|[卖木头块][2312] |![py]
13721373
[2315][2315l]|[统计星号][2315] |![rs]
13731374
[2317][2317l]|[操作后的最大异或和][2317] |![rs]
13741375
[2319][2319l]|[判断矩阵是否是一个 X 矩阵][2319] |![py]
@@ -2880,6 +2881,7 @@
28802881
[2306]:Problemset/2306-Naming%20a%20Company/README_CN.md#2306-公司命名
28812882
[2309]:Problemset/2309-Greatest%20English%20Letter%20in%20Upper%20and%20Lower%20Case/README_CN.md#2309-兼具大小写的最好英文字母
28822883
[2310]:Problemset/2310-Sum%20of%20Numbers%20With%20Units%20Digit%20K/README_CN.md#2310-个位数字为-k-的整数之和
2884+
[2312]:Problemset/2312-Selling%20Pieces%20of%20Wood/README_CN.md#2312-卖木头块
28832885
[2315]:Problemset/2315-Count%20Asterisks/README_CN.md#2315-统计星号
28842886
[2317]:Problemset/2317-Maximum%20XOR%20After%20Operations/README_CN.md#2317-操作后的最大异或和
28852887
[2319]:Problemset/2319-Check%20if%20Matrix%20Is%20X-Matrix/README_CN.md#2319-判断矩阵是否是一个-x-矩阵
@@ -4390,6 +4392,7 @@
43904392
[2306l]:https://leetcode.cn/problems/naming-a-company/
43914393
[2309l]:https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/
43924394
[2310l]:https://leetcode.cn/problems/sum-of-numbers-with-units-digit-k/
4395+
[2312l]:https://leetcode.cn/problems/selling-pieces-of-wood/
43934396
[2315l]:https://leetcode.cn/problems/count-asterisks/
43944397
[2317l]:https://leetcode.cn/problems/maximum-xor-after-operations/
43954398
[2319l]:https://leetcode.cn/problems/check-if-matrix-is-x-matrix/

0 commit comments

Comments
 (0)