当前位置:网站首页>2022.7.10-----leetcode.741
2022.7.10-----leetcode.741
2022-07-19 11:43:00 【路Lu727】
//题意可等价为两个人a,b同时走,到最后的最优解
static int N = 51, INF = Integer.MIN_VALUE;
static int[][][] f = new int[2 * N][N][N];//走了k步,a在i1行,b在i2行的解
public int cherryPickup(int[][] g) {
int n = g.length;
//初始化矩阵为负无穷,如果某位置的上一步不可到达,那么该位置也不可到达,转移求最大时舍弃
for (int k = 0; k <= 2 * n; k++) {
for (int i1 = 0; i1 <= n; i1++) {
for (int i2 = 0; i2 <= n; i2++) {
f[k][i1][i2] = INF;
}
}
}
//初始状态
f[2][1][1] = g[0][0];
//一共走2n-2步到终点
for (int k = 3; k <= 2 * n; k++) {
for (int i1 = 1; i1 <= n; i1++) {
for (int i2 = 1; i2 <= n; i2++) {
int j1 = k - i1, j2 = k - i2;//对于第k步,已知所在行,可求出所在列
//对于第k步,i和j的范围会有限制,剔除不在正确范围的状态
if (j1 <= 0 || j1 > n || j2 <= 0 || j2 > n) continue;
//获取a,b当前位置得分
int A = g[i1 - 1][j1 - 1], B = g[i2 - 1][j2 - 1];
//当前位置不可到达
if (A == -1 || B == -1) continue;
//对于两个人,上一步所在位置有四种组合
int a = f[k - 1][i1 - 1][i2], b = f[k - 1][i1 - 1][i2 - 1],
c = f[k - 1][i1][i2 - 1], d = f[k - 1][i1][i2];
//取上一步最大得分
int t = Math.max(Math.max(a, b), Math.max(c, d)) + A;
if (i1 != i2) t += B;//若当前两人在同一位置,不可重复计分
f[k][i1][i2] = t;
}
}
}
return f[2 * n][n][n] <= 0 ? 0 : f[2 * n][n][n];
}
边栏推荐
- 开源SPL强化MangoDB计算
- Comparison of eolink and JMeter interface tests
- uniapp项目启动报错 binding.node is not a valid Win32 application 终极解决方案
- mouseenter 与 mouseover 的区别
- 华人女婿小野三太成密西根大学首位亚裔校长,年薪超650万!
- CB Insights发布AI行业七大趋势:合成数据、多模态AI崛起
- 股票开户首选,炒股交易开户佣金最低手机上开户安不安全
- 浅析IM即时通讯开发中Bitmap到底占用多大内存?
- 【LeetCode】28、实现 strStr()
- 【职场规则】IT职场规则|工作能力差的表现
猜你喜欢
ICLR 2022 | GNNAsKernel: 能提升任意GNN表达能力的通用框架
Cloud Native Core Technology: Service Mesh Implementation - istio
Uncover the data enhancement potential of MAE, and Shanghai Communications & Huawei proposes masking reconstruction data enhancement based on MAE
Cooperatively Coevolving Particle Swarms forLarge Scale Optimization
性能领域:你知道的越多,不知道的也就越多
PL-VIO: Tightly-Coupled Monocular Visual–Inertial Odometry Using Point and Line Features
DevOps 实践多年,最痛的居然是?
test
揭秘MAE的数据增强潜力,上交&华为基于MAE提出掩蔽重建数据增强
C#/VB.NET 添加多行文本水印到Word文档
随机推荐
Week 5 Image Classification、Bag of Visual Words (Bag of Features) and Multi-Layer Neural Networks
CB insights released seven trends in the AI industry: synthetic data and the rise of multimodal AI
嵌入式开发:嵌入式基础——线程与任务
【云原生】Nacos-TaskManager 任务管理的使用
扫描删除文件夹问题
SAP 实施项目中涉及到编程方式操作 Excel 的几种场景介绍
OneFlow v0.8.0正式发布
【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
Redis支撐秒殺場景的關鍵技術和實踐都有哪些?
股票开户首选,炒股交易开户佣金最低手机上开户安不安全
【历史上的今天】7 月 13 日:数据库之父逝世;苹果公司购买 CUPS 代码;IBM 芯片联盟
Vulnhub | DC: 5 |【实战】
ICLR 2022 | GNNAsKernel: 能提升任意GNN表达能力的通用框架
c语言 文件读写
dataframe先分组运算再合并输出
如何用 UDP 实现可靠传输?
flink sql 配置 kafka 连锁一个多分区的topic 一直没连上也没报错,单分区的没问题
South Korean universities have arrived at the Consulate General in Gwangju in the summer to remind Chinese students to pay attention to safety
揭秘MAE的数据增强潜力,上交&华为基于MAE提出掩蔽重建数据增强
Xinlaifu Xincai IPO meeting: annual revenue of 780million, with 47% equity held by Wang Xiaoming