当前位置:网站首页>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];
}
};
边栏推荐
- Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
- Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
- Laravel artisan 常用命令
- 企业应该选择无服务器计算吗?
- NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
- Introduction to ffmpeg Lib
- Three core problems of concurrent programming
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- MySQL view concept, create view, view, modify view, delete view
猜你喜欢
734. Energy stone (greed, backpack)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
遊戲思考15:全區全服和分區分服的思考
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
城市选择器组件实现原理
Raspberry pie 4B learning notes - IO communication (1-wire)
Three core problems of concurrent programming
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
人工智能在网络安全中的作用
随机推荐
What are the skills of spot gold analysis?
电子协会 C语言 1级 33 、奇偶数判断
Feature extraction and detection 16 brisk feature detection and matching
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Automatically browse pinduoduo products
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
What is AQS and its principle
MySQL view concept, create view, view, modify view, delete view
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
JMeter (II) - install the custom thread groups plug-in
基于SSM实现微博系统
C language 3-7 daffodils (enhanced version)
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Have you stepped on the nine common pits in the e-commerce system?
The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
Introduction to ffmpeg Lib
ES6 new method of string
分卷压缩,解压
The concept, function, characteristics, creation and deletion of MySQL constraints
人工智能在网络安全中的作用