当前位置:网站首页>LeetCode 2312. Sell Wood Blocks
LeetCode 2312. Sell Wood Blocks
2022-08-02 07:49:00 【HumbleFool】
LeetCode 2312. 卖木头块
暴力递归 TLE
const int N = 210;
typedef long long LL;
class Solution {
public:
int price[N][N] = {
0};
LL dfs(int n, int m)
{
if(n == 0 || m == 0)
return 0;
//The whole piece is not separated
LL p1 = price[n][m];
// 按行分
LL p2 = 0;
for(int i = 1; i < n; i ++)
{
LL up = dfs(i, m);
LL down = dfs(n - i, m);
p2 = max(p2, up + down);
}
// 按列分
LL p3 = 0;
for(int i = 1; i < m; i ++)
{
LL left = dfs(n, i);
LL right = dfs(n, m - i);
p3 = max(p3, left + right);
}
return max(p1, max(p2, p3));
}
long long sellingWood(int n, int m, vector<vector<int>>& prices) {
// A two-dimensional table stores prices
for(auto&& p : prices)
price[p[0]][p[1]] = p[2];
return dfs(n, m);
}
};
记忆化搜索 AC
const int N = 210;
typedef long long LL;
class Solution {
public:
int price[N][N] = {
0};
LL f[N][N];
LL dfs(int n, int m)
{
// 0行或者0列
if(n == 0 || m == 0)
return 0;
// 记忆化
if(f[n][m] != -1)
return f[n][m];
//The whole piece is not separated
LL p1 = price[n][m];
// 按行分
LL p2 = 0;
for(int i = 1; i < n; i ++)
{
LL up = dfs(i, m);
LL down = dfs(n - i, m);
p2 = max(p2, up + down);
}
// 按列分
LL p3 = 0;
for(int i = 1; i < m; i ++)
{
LL left = dfs(n, i);
LL right = dfs(n, m - i);
p3 = max(p3, left + right);
}
f[n][m] = max(p1, max(p2, p3));
return f[n][m];
}
long long sellingWood(int n, int m, vector<vector<int>>& prices) {
// 2D record price
for(auto&& p : prices)
price[p[0]][p[1]] = p[2];
memset(f, -1, sizeof f);
return dfs(n, m);
}
};
Elegant dynamic programming
const int N = 210;
typedef long long LL;
class Solution {
public:
LL f[N][N] = {
0}; //表示i行jThe maximum block size of the column
long long sellingWood(int n, int m, vector<vector<int>>& prices) {
// 初始化
for(auto&& p : prices)
f[p[0]][p[1]] = p[2];
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
// 按行分割
for(int k = 1; k <= (i >> 1); k ++)
f[i][j] = max(f[i][j], f[k][j] + f[i - k][j]);
// 按列分割
for(int k = 1; k <= (j >> 1); k ++)
f[i][j] = max(f[i][j], f[i][k] + f[i][j - k]);
}
return f[n][m];
}
};
边栏推荐
- 【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
- 实验7 MPLS实验
- 【暑期每日一题】洛谷 P1255 数楼梯
- 【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
- 【请教】SQL语句按列1去重来计算列2之和
- Agile, DevOps and Embedded Systems Testing
- (Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
- 59:第五章:开发admin管理服务:12:MongoDB的使用场景;(非核心数据,数据量比较大的非核心数据,人脸照片等隐私的小文件;)
- docker 安装mysql
- _2_顺序表
猜你喜欢
随机推荐
【机器学习】实验2布置:基于回归分析的大学综合得分预测
【机器学习】课程设计布置:某闯关类手游用户流失预测
About the SQL concat () function problem, how to splice
View port number occupancy
【心电信号】基于matlab心率检测【含Matlab源码 1993期】
【红队】ATT&CK - 创建或修改系统进程实现持久化(更新ing)
MPLS的相关技术
OC - NSSet (set)
获取间隔的日期列表工具类
【机器学习】实验6布置:基于集成学习的Amazon用户评论质量预测
雷达人体存在感应器方案,智能物联网感知技术,实时感应人体存在
C#重点问题之Struct和Class的异同
CSRF-跨站请求伪造-相关知识
海缆探测仪TSS350(二)
自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
MySQL-FlinkCDC-Hudi实时入湖
【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
逆变器绝缘检测检测功能及软件实现
Xilinx约束学习笔记—— 时序约束









