Skip to content

Commit 2a4dc37

Browse files
author
robot
committed
feat: $5224
1 parent 831123e commit 2a4dc37

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
538538
- [2209. 用地毯覆盖后的最少白色砖块](./problems/2209.minimum-white-tiles-after-covering-with-carpets.md) 👍
539539
- [2281. 巫师的总力量和](./problems/2281.sum-of-total-strength-of-wizards.md)
540540
- [2306. 公司命名](./problems/2306.naming-a-company.md) 枚举优化好题
541+
- [5254. 卖木头块](./problems/5254.selling-pieces-of-wood.md) 动态规划经典题
541542
- [5999. 统计数组中好三元组数目](./problems/5999.count-good-triplets-in-an-array.md) 👍
542543

543544
## :trident:  anki 卡片

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@
353353
- [2209. 用地毯覆盖后的最少白色砖块](./problems/2209.minimum-white-tiles-after-covering-with-carpets.md)
354354
- [2281.sum-of-total-strength-of-wizards](./problems/2281.sum-of-total-strength-of-wizards.md)
355355
- [2306. 公司命名](./problems/2306.naming-a-company.md) 枚举优化好题
356+
- [5254. 卖木头块](./problems/5254.selling-pieces-of-wood.md) 动态规划经典题
356357
- [5999. 统计数组中好三元组数目](./problems/5999.count-good-triplets-in-an-array.md) 👍
357358

358359
- [后序](epilogue.md)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
## 题目地址(5254. 卖木头块)
2+
3+
https://leetcode.cn/problems/selling-pieces-of-wood/
4+
5+
## 题目描述
6+
7+
```
8+
给你两个整数 m 和 n ,分别表示一块矩形木块的高和宽。同时给你一个二维整数数组 prices ,其中 prices[i] = [hi, wi, pricei] 表示你可以以 pricei 元的价格卖一块高为 hi 宽为 wi 的矩形木块。
9+
10+
每一次操作中,你必须按下述方式之一执行切割操作,以得到两块更小的矩形木块:
11+
12+
沿垂直方向按高度 完全 切割木块,或
13+
沿水平方向按宽度 完全 切割木块
14+
15+
在将一块木块切成若干小木块后,你可以根据 prices 卖木块。你可以卖多块同样尺寸的木块。你不需要将所有小木块都卖出去。你 不能 旋转切好后木块的高和宽。
16+
17+
请你返回切割一块大小为 m x n 的木块后,能得到的 最多 钱数。
18+
19+
注意你可以切割木块任意次。
20+
21+
 
22+
23+
示例 1:
24+
25+
输入:m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
26+
输出:19
27+
解释:上图展示了一个可行的方案。包括:
28+
- 2 块 2 x 2 的小木块,售出 2 * 7 = 14 元。
29+
- 1 块 2 x 1 的小木块,售出 1 * 3 = 3 元。
30+
- 1 块 1 x 4 的小木块,售出 1 * 2 = 2 元。
31+
总共售出 14 + 3 + 2 = 19 元。
32+
19 元是最多能得到的钱数。
33+
34+
35+
示例 2:
36+
37+
输入:m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
38+
输出:32
39+
解释:上图展示了一个可行的方案。包括:
40+
- 3 块 3 x 2 的小木块,售出 3 * 10 = 30 元。
41+
- 1 块 1 x 4 的小木块,售出 1 * 2 = 2 元。
42+
总共售出 30 + 2 = 32 元。
43+
32 元是最多能得到的钱数。
44+
注意我们不能旋转 1 x 4 的木块来得到 4 x 1 的木块。
45+
46+
 
47+
48+
提示:
49+
50+
1 <= m, n <= 200
51+
1 <= prices.length <= 2 * 104
52+
prices[i].length == 3
53+
1 <= hi <= m
54+
1 <= wi <= n
55+
1 <= pricei <= 106
56+
所有 (hi, wi) 互不相同 。
57+
```
58+
59+
## 前置知识
60+
61+
- 动态规划记忆化递归
62+
63+
## 公司
64+
65+
- 暂无
66+
67+
## 思路
68+
69+
这是一个经典的枚举割点的动态规划问题。
70+
71+
相关题目有铺地毯/瓷砖,本质都是给你一个二维矩阵,给你一堆价值,让你求如何分割价值最小或最大。
72+
73+
可以这么做的前提是如果我们可以切割,那么切割后会变为两个子矩阵,这两个子矩阵和切割前除了大小不一样,其他都一样。因此可以不断枚举割点,递归解决。
74+
75+
定义 dp[i][j] 为切割长度为 i 宽度为 j 的木板的最大价格,那么答案就是 dp[m,n]
76+
77+
接下来,我们枚举横着切的切点和竖着切的切点就可以得到答案。
78+
79+
切割前我们有三种选择:
80+
81+
1. 横着切,切哪呢?枚举所有可能。因为横着切本质是高度变了,宽度不变,因此枚举所有可能就是枚举高度为 [1,i-1](其中 i 为当前木板高度)
82+
2. 竖着切,同理
83+
3. 不切。
84+
85+
取三种情况的最大值即可。
86+
87+
## 关键点
88+
89+
- 枚举切割点
90+
91+
## 代码
92+
93+
- 语言支持:Python3
94+
95+
Python3 Code:
96+
97+
```python
98+
99+
class Solution:
100+
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
101+
d = {(h, w): p for h, w, p in prices}
102+
@cache
103+
def dp(i, j):
104+
ans = d.get((i, j), 0) # 不切
105+
# 竖着切
106+
for x in range(1, i):
107+
ans = max(ans, dp(x, j) + dp(i - x, j))
108+
# 横着切
109+
for y in range(1, j):
110+
ans = max(ans, dp(i, y) + dp(i, j - y))
111+
return ans # 且三种选择的最大值即可
112+
return dp(m, n)
113+
114+
```
115+
116+
**复杂度分析**
117+
118+
令 t 为 prices 长度。
119+
120+
- 时间复杂度:$O(n * m * (n + m))$
121+
- 空间复杂度:$O(t + n * m)$
122+
123+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
124+
125+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
126+
127+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
128+
129+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
130+
131+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)