当前位置:网站首页>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];
}
};
边栏推荐
- 机器学习基本概念
- Construction and maintenance of business websites [15]
- Electronic Association C language level 1 33, odd even number judgment
- 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
- Convolutional neural network (including code and corresponding diagram)
- MySQL约束与多表查询实例分析
- Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
- leetcode2312. 卖木头块(困难,周赛)
- 医药管理系统(大一下C语言课设)
猜你喜欢

Data analysis on the disaster of Titanic

Memorabilia of domestic database in June 2022

开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?

Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?

Discussion on the idea of platform construction

医药管理系统(大一下C语言课设)

Word search applet design report based on cloud development +ppt+ project source code + demonstration video

Android high frequency network interview topic must know and be able to compare Android development environment

Learn about servlets

The concept, function, characteristics, creation and deletion of MySQL constraints
随机推荐
There are spaces in the for loop variable in the shell -- IFS variable
如何用一款产品推动「品牌的惊险一跃」?
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
如何远程、在线调试app?
Construction and maintenance of business websites [13]
Openssl3.0 learning XXI provider encoder
This is the form of the K-line diagram (pithy formula)
Ubuntu20.04 PostgreSQL 14 installation configuration record
Discussion on the idea of platform construction
Experimental reproduction of variable image compression with a scale hyperprior
Ks006 student achievement management system based on SSM
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
The concept, function, characteristics, creation and deletion of MySQL constraints
Matlab uses resample to complete resampling
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
new和malloc的区别
自动浏览拼多多商品
Implementation of Weibo system based on SSM
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory