当前位置:网站首页>LeetCode 2312. 卖木头块
LeetCode 2312. 卖木头块
2022-08-02 06:52: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;
//整块不分开
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) {
// 二维表存储价格
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];
//整块不分开
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) {
// 二维记录价格
for(auto&& p : prices)
price[p[0]][p[1]] = p[2];
memset(f, -1, sizeof f);
return dfs(n, m);
}
};
优雅动态规划
const int N = 210;
typedef long long LL;
class Solution {
public:
LL f[N][N] = {
0}; //表示i行j列的方块最大值
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];
}
};
边栏推荐
猜你喜欢

数据库概论-MySQL的数据表的基本操作

21 days learning challenge 】 【 sequential search

Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem

埋点开发流程

实例028:递归求等差数列

Specified URL is not reachable,caused by :‘Read timed out

File upload vulnerability (2)

59:第五章:开发admin管理服务:12:MongoDB的使用场景;(非核心数据,数据量比较大的非核心数据,人脸照片等隐私的小文件;)

optional

OC-错误提示
随机推荐
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
查看僵尸进程
[21天学习挑战赛——内核笔记](一)——设备树的概述(硬件、目标、效果、文件类型)
反射课后习题及做题记录
获取间隔的日期列表工具类
MySQL-FlinkCDC-Hudi实时入湖
你认同这个观点吗?大多数企业的数字化都只是为了缓解焦虑
(2022牛客多校五)D-Birds in the tree(树形DP)
2020美亚团队赛复盘
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
July 18-July 31, 2022 (Ue4 video tutorials and documentation, 20 hours. Total 1412 hours, 8588 hours left)
埋点开发流程
awk语法-01-基础语法(命令、选项、内部变量)
OC-NSNumber和NSValue一般用来装箱拆箱
Resolving C# non-static field, method or property "islandnum.Program.getIslandCount(int[][], int, int)" requires an object reference
牛客编程题中——需要处理输入较大数的题目
图腾柱和推挽电路介绍
【机器学习】实验3布置:贝叶斯垃圾邮件识别
Link with Game Glitch(spfa判负环)
实例027:递归输出