当前位置:网站首页>最大路径和
最大路径和
2022-07-31 01:38:00 【小卢要刷力扣题】
前言
链接:https://www.nowcoder.com/questionTerminal/8ecfe02124674e908b2aae65aad4efdf
来源:牛客网
给定一个矩阵matrix,先从左上角开始,每一步只能往右或者往下走,走到右下角。然后从右下角出发,每一步只能往上或者往左走,再回到左上角。任何一个位置的数字,只能获得一遍。返回最大路径和。
输入描述:
第一行输入两个整数M和N,M,N<=200
接下来M行,每行N个整数,表示矩阵中元素
输出描述:
输出一个整数,表示最大路径和
解题思路
两个人A, B都从左下角走到右下角,都只能向下或者向右走,但是A跟B能做出不同的选择
如果,某一时刻,AB进入相同的一 个格子,A和B只获得一份
A走到之后,就认为B就是回来的路径
A来到了a行b列, B来到了c行d列,如果它们跳进不同的格子里。
只获得一个的情况下,问你a跟b获得整体的最大。
如果某一个位置A也来过,B也来过,AB-定是同时来的,而不会分先后
因为AB同步走
可以省掉一个变量d,可以根据abc来推出d
代码
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[][] matrix = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
matrix[i][j] = sc.nextInt();
}
}
int ans = cherryPickup(matrix);
System.out.println(ans);
sc.close();
}
public static int cherryPickup(int[][] grid) {
int N = grid.length;
int M = grid[0].length;
int[][][] dp = new int[N][M][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
dp[i][j][k] = Integer.MIN_VALUE;
}
}
}
int ans = process(grid, 0, 0, 0, dp);
return ans < 0 ? 0 : ans;
}
public static int process(int[][] grid, int x1, int y1, int x2, int[][][] dp) {
if (x1 == grid.length || y1 == grid[0].length || x2 == grid.length || x1 + y1 - x2 == grid[0].length) {
return Integer.MIN_VALUE;
}
if (dp[x1][y1][x2] != Integer.MIN_VALUE) {
return dp[x1][y1][x2];
}
if (x1 == grid.length - 1 && y1 == grid[0].length - 1) {
dp[x1][y1][x2] = grid[x1][y1];
return dp[x1][y1][x2];
}
int next = Integer.MIN_VALUE;
next = Math.max(next, process(grid, x1 + 1, y1, x2 + 1, dp));
next = Math.max(next, process(grid, x1 + 1, y1, x2, dp));
next = Math.max(next, process(grid, x1, y1 + 1, x2, dp));
next = Math.max(next, process(grid, x1, y1 + 1, x2 + 1, dp));
if (grid[x1][y1] == -1 || grid[x2][x1 + y1 - x2] == -1 || next == -1) {
dp[x1][y1][x2] = -1;
return dp[x1][y1][x2];
}
if (x1 == x2) {
dp[x1][y1][x2] = grid[x1][y1] + next;
return dp[x1][y1][x2];
}
dp[x1][y1][x2] = grid[x1][y1] + grid[x2][x1 + y1 - x2] + next;
return dp[x1][y1][x2];
}
边栏推荐
猜你喜欢
leetcode-952:按公因数计算最大组件大小
The sword refers to offer17---print the n digits from 1 to the largest
斩获BAT、TMD技术专家Offer,我都经历了什么?
【952. Calculate the maximum component size according to the common factor】
The difference between 4G communication module CAT1 and CAT4
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
rpm install postgresql12
35. 反转链表
JPEG Steganalysis of Digital Image Steganography
Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II
随机推荐
《实战》基于电商领域的词性提取及其决策树模型建模
TiDB 在多点数字化零售场景下的应用
初识C语言 -- 数组
聚簇索引和非聚簇索引到底有什么区别
想要写出好的测试用例,先要学会测试设计
调度中心xxl-Job
射频器件的基本参数1
tensorflow与GPU版本对应安装问题
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
【flask入门系列】Flask-SQLAlchemy的使用
认识DTU什么是4GDTU设备
进程间通信学习笔记
System design. Short chain system design
力扣每日一题-第46天-704. 二分查找
Sping.事务的传播特性
MySQL (6)
蛮力法/邻接表 广度优先 有向带权图 无向带权图
斩获BAT、TMD技术专家Offer,我都经历了什么?
Crawler text data cleaning
1782. 统计点对的数目 双指针