当前位置:网站首页>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];
}
};
边栏推荐
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
- Exception handling of class C in yyds dry goods inventory
- Deep learning: a solution to over fitting in deep neural networks
- Selection of field types for creating tables in MySQL database
- The role of artificial intelligence in network security
- Electronic Association C language level 1 33, odd even number judgment
- Six lessons to be learned for the successful implementation of edge coding
- * and & symbols in C language
- Basic concepts of machine learning
- 【LeetCode 43】236. The nearest common ancestor of binary tree
猜你喜欢

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

This is the form of the K-line diagram (pithy formula)

What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
![[Maya] the error of importing Maya into Metahuman](/img/46/46bd1c2d507c9e48ef8c066c54231d.jpg)
[Maya] the error of importing Maya into Metahuman

The role of artificial intelligence in network security

Implementation principle of city selector component

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

Design and implementation of key value storage engine based on LSM tree

The technology boss is ready, and the topic of position C is up to you

Three core problems of concurrent programming
随机推荐
The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
Redis有序集合如何使用
电子协会 C语言 1级 33 、奇偶数判断
Memorabilia of domestic database in June 2022
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
Matlab uses resample to complete resampling
Makefile simple induction
How to use a product to promote "brand thrill"?
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
Six lessons to be learned for the successful implementation of edge coding
Based on configured schedule, the given trigger will never fire
Volume compression, decompression
leetcode2305. 公平分发饼干(中等,周赛,状压dp)
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
[Maya] the error of importing Maya into Metahuman
迁移云计算工作负载的四个基本策略
[question] - why is optical flow not good for static scenes
Experimental reproduction of variable image compression with a scale hyperprior
new和malloc的区别