当前位置:网站首页>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];
}
};
边栏推荐
- Deep learning: a solution to over fitting in deep neural networks
- Raspberry pie 4B learning notes - IO communication (1-wire)
- It's already 30. Can you learn programming from scratch?
- 479. Additive binary tree (interval DP on the tree)
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- Implementation of Weibo system based on SSM
- D discard the virtual recovery method
- MySQL view concept, create view, view, modify view, delete view
- Architecture evolution from MVC to DDD
- Basic concepts of machine learning
猜你喜欢

SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5

Self drawing of menu items and CListBox items

Implementation principle of city selector component

Learn basic K-line diagram knowledge in three minutes

leetcode2305. 公平分发饼干(中等,周赛,状压dp)

leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)

Cross domain? Homology? Understand what is cross domain at once

Introduction to ffmpeg Lib

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

479. Additive binary tree (interval DP on the tree)
随机推荐
What are the skills of spot gold analysis?
自动浏览拼多多商品
城市选择器组件实现原理
Game thinking 15: thinking about the whole region and sub region Services
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
734. Energy stone (greed, backpack)
Design and implementation of key value storage engine based on LSM tree
Three core problems of concurrent programming
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
10 minutes to get started quickly composition API (setup syntax sugar writing method)
三分钟学会基础k线图知识
C language 3-7 daffodils (enhanced version)
现货黄金分析的技巧有什么呢?
Five skills of adding audio codec to embedded system
[Floyd] post disaster reconstruction
ES6 new method of string
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
Android: the kotlin language uses grendao3, a cross platform app development framework
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
牛客网——华为题库(51~60)