当前位置:网站首页>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];
}
};
边栏推荐
- Automatically browse pinduoduo products
- Four basic strategies for migrating cloud computing workloads
- 分卷压缩,解压
- Medical management system (C language course for freshmen)
- 2022 Q2 - 提升技能的技巧总结
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- ES6 new method of string
- Self drawing of menu items and CListBox items
- The role of artificial intelligence in network security
- 机器学习基本概念
猜你喜欢
卷積神經網絡(包含代碼與相應圖解)
Feature extraction and detection 16 brisk feature detection and matching
城市选择器组件实现原理
Redis有序集合如何使用
MySQL中一条SQL是怎么执行的
遷移雲計算工作負載的四個基本策略
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
人工智能在网络安全中的作用
JMeter (II) - install the custom thread groups plug-in
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
随机推荐
基于SSM实现微博系统
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Learn basic K-line diagram knowledge in three minutes
现货黄金分析的技巧有什么呢?
MySQL view concept, create view, view, modify view, delete view
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
Using tabbar in wechat applet
成功实现边缘编码需要了解的六大经验教训
Should enterprises choose server free computing?
The role of artificial intelligence in network security
Based on configured schedule, the given trigger will never fire
Number of palindromes in C language (leetcode)
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
uTools
[C #] use regular verification content
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
如何用一款产品推动「品牌的惊险一跃」?
Software No.1
How to debug apps remotely and online?