当前位置:网站首页>leetcode2312. Selling wood blocks (difficult, weekly race)
leetcode2312. Selling wood blocks (difficult, weekly race)
2022-07-02 01:54:00 【Heavy garbage】


Ideas :dp
How to think of dp Of ?
vertical / Cut horizontally , obtain Smaller subproblems
First vertical then horizontal and first horizontal then vertical will Get two pieces of wood of the same size , therefore 1: There are duplicate subproblems
For the same height and width The maximum amount of money obtained from two blocks has nothing to do with the process of obtaining these two blocks , therefore 2: No aftereffect
get The maximum amount of money for two small wooden blocks , You get the maximum amount of money for large wooden blocks , therefore 3: Optimal substructure
Meet the above three conditions , therefore ->dp
State shift :
p[m][n] Express high m wide n The maximum amount of money you can get from a piece of wood
dp[m][n] You can get one if you can sell it directly 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
Recursive termination :dp[1][1]
Code implementation ? Because it will traverse all States , Therefore, it is realized by recursion
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];
}
};
边栏推荐
- 电子协会 C语言 1级 32、计算2的幂
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
- JMeter (I) - download, installation and plug-in management
- 三分钟学会基础k线图知识
- The role of artificial intelligence in network security
- [Maya] the error of importing Maya into Metahuman
- Construction and maintenance of business websites [15]
- Failed to transform file 'xxx' to match attributes
- 迁移云计算工作负载的四个基本策略
- Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
猜你喜欢
![[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

Introduction to ffmpeg Lib

MySQL中一条SQL是怎么执行的

如何用一款产品推动「品牌的惊险一跃」?

现货黄金分析的技巧有什么呢?

Volume compression, decompression

Learn basic K-line diagram knowledge in three minutes

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

matlab 实现语音信号重采样和归一化,并播放比对效果
随机推荐
What is AQS and its principle
2022 Q2 - résumé des compétences pour améliorer les compétences
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
Based on configured schedule, the given trigger will never fire
MySQL中一条SQL是怎么执行的
卷積神經網絡(包含代碼與相應圖解)
Five skills of adding audio codec to embedded system
Game thinking 15: thinking about the whole region and sub region Services
Basic concepts of machine learning
Construction and maintenance of business websites [12]
城市选择器组件实现原理
D discard the virtual recovery method
k线图形态这样记(口诀篇)
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Ks006 student achievement management system based on SSM
Automatically browse pinduoduo products
Exception handling of class C in yyds dry goods inventory
企业应该选择无服务器计算吗?
Volume compression, decompression
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port