当前位置:网站首页>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];
}
};
边栏推荐
- 牛客网——华为题库(51~60)
- Architecture evolution from MVC to DDD
- Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
- 2022 Q2 - Summary of skills to improve skills
- Should enterprises choose server free computing?
- Based on configured schedule, the given trigger will never fire
- 迁移云计算工作负载的四个基本策略
- Basic concepts of machine learning
- leetcode373. 查找和最小的 K 对数字(中等)
- 2022 Q2 - 提昇技能的技巧總結
猜你喜欢

城市选择器组件实现原理

Redis有序集合如何使用

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

leetcode373. 查找和最小的 K 对数字(中等)

Matlab uses resample to complete resampling

卷積神經網絡(包含代碼與相應圖解)
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/ba/dcb276768b1a9cc84099f093677d29.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

matlab 使用 resample 完成重采样

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

MySQL约束与多表查询实例分析
随机推荐
ES6 new method of string
Automatically browse pinduoduo products
迁移云计算工作负载的四个基本策略
uTools
2022 Q2 - Summary of skills to improve skills
Data analysis on the disaster of Titanic
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
Have you stepped on the nine common pits in the e-commerce system?
Six lessons to be learned for the successful implementation of edge coding
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
人工智能在网络安全中的作用
It's already 30. Can you learn programming from scratch?
PR second training
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
成功实现边缘编码需要了解的六大经验教训
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
遊戲思考15:全區全服和分區分服的思考
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)