当前位置:网站首页>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];
}
};
边栏推荐
- MySQL如何解决delete大量数据后空间不释放的问题
- SQLite 3 of embedded database
- [rust web rokcet Series 2] connect the database and add, delete, modify and check curd
- Leetcode, 3 repeatless longest subsequence
- [rust web rokcet Series 1] Hello, world and get, post, put, delete
- Brief introduction to the development of mobile network
- Three core problems of concurrent programming
- [Maya] the error of importing Maya into Metahuman
- 医药管理系统(大一下C语言课设)
- uTools
猜你喜欢

6-2 vulnerability exploitation - inevitable problems of FTP

PR second training

Redis有序集合如何使用

Convolutional neural network (including code and corresponding diagram)

The concept, function, characteristics, creation and deletion of MySQL constraints

Learning note 24 - multi sensor post fusion technology

Learning notes 25 - multi sensor front fusion technology

The technology boss is ready, and the topic of position C is up to you

如何用一款产品推动「品牌的惊险一跃」?
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
随机推荐
Discussion on the idea of platform construction
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
Raspberry pie 4B learning notes - IO communication (1-wire)
如何远程、在线调试app?
NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
Exclusive delivery of secret script move disassembly (the first time)
人工智能在网络安全中的作用
Parted command
Makefile simple induction
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
游戏思考15:全区全服和分区分服的思考
牛客网——华为题库(51~60)
KS006基于SSM实现学生成绩管理系统
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
1217 supermarket coin processor
Automatically browse pinduoduo products
Construction and maintenance of business websites [11]
Brief introduction to the development of mobile network
Construction and maintenance of business websites [14]
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?