【发布时间】:2022-07-02 11:36:36
【问题描述】:
我正在尝试解决 leetcode.com 上的以下problem
我发现resource 提出了类似的问题,但没有得出任何合理的结论。
我能够提出一个递归回溯解决方案,但是对于一个非常大的矩阵,它在最后 5 个测试用例中失败了。我感觉有一些重复的工作,但不知道是什么。
逻辑的写法,苦苦摸索
- 我需要记住的内容
- 如何调整我的逻辑以便我在每一步都进行优化
感谢任何帮助
class Solution {
private long max = -1;
public int maxProductPath(int[][] grid) {
long mod=1000000007;
int rows = grid.length;
int cols = grid[0].length;
int cp = 1;
pathFinder(grid, rows, cols, cp, rows-1, cols-1);
if(max < 0 ) return -1;
return (int)(max % mod);
}
public void pathFinder(int[][]grid, int rows, int cols, long cp, int r, int c){
if(r >= rows || c >= cols || r < 0 || c < 0){
return;
}
if(r == 0 && c == 0){
this.max = Math.max(cp * grid[r][c] , this.max);
return;
}
pathFinder(grid, rows, cols, cp * grid[r][c], r - 1, c);
pathFinder(grid, rows, cols, cp * grid[r][c], r , c - 1);
}
}
【问题讨论】:
标签: algorithm recursion graph dynamic-programming backtracking