当前位置:网站首页>leetcode2312. 卖木头块(困难,周赛)
leetcode2312. 卖木头块(困难,周赛)
2022-07-02 01:51:00 【重you小垃】


思路:dp
如何想到dp的?
垂直/水平切割,得到更小的子问题
先垂直再水平和先水平再垂直会得到同样大小的两块木块,因此1:存在重复子问题
对于同样高和宽的两个木块所获得的最多钱数和得到这两个木块的过程无关,因此2:无后效性
获得两块小木块的最多钱数,就获得了大木块的最多钱数,因此3:最优子结构
满足以上三个条件,因此->dp
状态转移:
p[m][n]表示高m宽n的木块能获得的最大钱数
dp[m][n]能直接卖的话获得一个price
dp[m][n]=max(dp[m][j]+dp[m][n-j]),1<=j<=n-1
dp[m][n]=max(dp[j][n]+dp[m-j][n]),1<=j<=m-1
递归终止:dp[1][1]
代码实现方式?由于会遍历到所有的状态,因此用递推实现
class Solution {
public:
long long sellingWood(int m, int n, vector<vector<int>>& prices) {
vector<vector<long long>> dp(m + 1, vector<long long>(n + 1)); //long long
auto p = dp;
for (auto& a : prices) p[a[0]][a[1]] = a[2];
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
dp[i][j] = p[i][j];
for (int k = 1; k <= j - 1; ++k) dp[i][j] = max(dp[i][j], dp[i][k] + dp[i][j - k]);
for (int k = 1; k <= i - 1; ++k) dp[i][j] = max(dp[i][j], dp[k][j] + dp[i-k][j]);
}
}
return dp[m][n];
}
};
边栏推荐
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- Fastadmin controls the length of fields in the table
- JMeter (II) - install the custom thread groups plug-in
- The difference between new and malloc
- Have you stepped on the nine common pits in the e-commerce system?
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- Construction and maintenance of business websites [13]
- What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
- Using mongodb in laravel
猜你喜欢

Memorabilia of domestic database in June 2022

What are the skills of spot gold analysis?

Implementation principle of city selector component

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
![[rust web rokcet Series 2] connect the database and add, delete, modify and check curd](/img/23/cfa13ad30e45ef7cdda690775964a7.jpg)
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
![[rust web rokcet Series 1] Hello, world and get, post, put, delete](/img/d8/7dd5fe409d349a13128b6af554f952.jpg)
[rust web rokcet Series 1] Hello, world and get, post, put, delete

k线图形态这样记(口诀篇)

Penser au jeu 15: penser au service complet et au sous - service

1222. Password dropping (interval DP, bracket matching)

Game thinking 15: thinking about the whole region and sub region Services
随机推荐
Exception handling of class C in yyds dry goods inventory
Have you stepped on the nine common pits in the e-commerce system?
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
成功实现边缘编码需要了解的六大经验教训
k线图形态这样记(口诀篇)
[Floyd] post disaster reconstruction
Learning note 24 - multi sensor post fusion technology
Ubuntu20.04 PostgreSQL 14 installation configuration record
uTools
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Volume compression, decompression
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
MySQL如何解决delete大量数据后空间不释放的问题
【C#】使用正则校验内容
Based on configured schedule, the given trigger will never fire
Memorabilia of domestic database in June 2022
Architecture evolution from MVC to DDD
The technology boss is ready, and the topic of position C is up to you
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3