当前位置:网站首页>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];
}
};
边栏推荐
- 机器学习基本概念
- np. Where and torch Where usage
- How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
- Number of palindromes in C language (leetcode)
- Raspberry pie 4B learning notes - IO communication (1-wire)
- Construction and maintenance of business websites [11]
- Construction and maintenance of business websites [13]
- MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
- Feature extraction and detection 16 brisk feature detection and matching
- leetcode2312. 卖木头块(困难,周赛)
猜你喜欢

MPLS experiment operation

How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?

如何用一款产品推动「品牌的惊险一跃」?

Discussion on the idea of platform construction

leetcode2312. 卖木头块(困难,周赛)

Data analysis on the disaster of Titanic

城市选择器组件实现原理

机器学习基本概念

leetcode2310. 个位数字为 K 的整数之和(中等,周赛)

Learn basic K-line diagram knowledge in three minutes
随机推荐
mysql列转行函数指的是什么
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
如何用一款产品推动「品牌的惊险一跃」?
Six lessons to be learned for the successful implementation of edge coding
Four basic strategies for migrating cloud computing workloads
Is the knowledge of University useless and outdated?
Self drawing of menu items and CListBox items
Should enterprises choose server free computing?
Deep learning: a solution to over fitting in deep neural networks
遷移雲計算工作負載的四個基本策略
遊戲思考15:全區全服和分區分服的思考
Construction and maintenance of business websites [15]
企业应该选择无服务器计算吗?
Construction and maintenance of business websites [10]
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
The concept, function, characteristics, creation and deletion of MySQL constraints
SQLite 3 of embedded database
[C #] use regular verification content