当前位置:网站首页>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];
}
};
边栏推荐
- Raspberry pie 4B learning notes - IO communication (1-wire)
- [rust web rokcet Series 2] connect the database and add, delete, modify and check curd
- TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
- Implementation principle of city selector component
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- Construction and maintenance of business websites [12]
- 2022 Q2 - résumé des compétences pour améliorer les compétences
- How to debug apps remotely and online?
- 并发编程的三大核心问题
- PR second training
猜你喜欢

MySQL如何解决delete大量数据后空间不释放的问题

卷积神经网络(包含代码与相应图解)

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages

【LeetCode 43】236. The nearest common ancestor of binary tree

It's already 30. Can you learn programming from scratch?

Introduction to ffmpeg Lib

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

Learning note 3 -- Key Technologies of high-precision map (Part 1)

How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
![[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
随机推荐
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
uTools
Construction and maintenance of business websites [11]
2022 Q2 - résumé des compétences pour améliorer les compétences
Learning note 3 -- Key Technologies of high-precision map (Part 1)
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
matlab 实现语音信号重采样和归一化,并播放比对效果
Android high frequency network interview topic must know and be able to compare Android development environment
城市选择器组件实现原理
如何用一款产品推动「品牌的惊险一跃」?
Android: the kotlin language uses grendao3, a cross platform app development framework
Redis有序集合如何使用
[Floyd] post disaster reconstruction
6-2 vulnerability exploitation - inevitable problems of FTP
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
Game thinking 15: thinking about the whole region and sub region Services
MySQL如何解决delete大量数据后空间不释放的问题
5g/4g pole gateway_ Smart pole gateway
Discussion on the idea of platform construction
Construction and maintenance of business websites [15]