力扣2684---矩阵中移动的最大次数(DFS,Java、中等题)

目录

题目描述:

思路描述:

代码:

纯递归:

带有记忆化搜索的递归:


题目描述:

给你一个下标从 0 开始、大小为 m x n 的矩阵 grid ,矩阵由若干 整数组成。

你可以从矩阵第一列中的 任一 单元格出发,按以下方式遍历 grid

  • 从单元格 (row, col) 可以移动到 (row - 1, col + 1)(row, col + 1)(row + 1, col + 1) 三个单元格中任一满足值 严格 大于当前单元格的单元格。

返回你在矩阵中能够 移动最大 次数。

示例 1:

复制代码
输入:grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]
输出:3
解释:可以从单元格 (0, 0) 开始并且按下面的路径移动:
- (0, 0) -> (0, 1).
- (0, 1) -> (1, 2).
- (1, 2) -> (2, 3).
可以证明这是能够移动的最大次数。

示例 2:

复制代码
复制代码
输入:grid = [[3,2,4],[2,1,9],[1,1,7]]
输出:0
解释:从第一列的任一单元格开始都无法移动。

提示:

  • m == grid.length
  • n == grid[i].length
  • 2 <= m, n <= 1000
  • 4 <= m * n <= 10^5
  • 1 <= grid[i][j] <= 10^6

思路描述:

从第一列开始进行有条件的深度优先搜索即可,如果纯递归的话,会超时。

递归加上记忆化搜索,既可以以比较好的时间复杂度通过了。

代码:

纯递归:

java 复制代码
public class Solation{
    /**
     * 从单元格 (row, col) 可以移动到 (row - 1, col + 1)、(row, col + 1) 和 (row + 1, col + 1) 三个单元格中任一满足值 严格 大于当前单元格的单元格。
     * 返回你在矩阵中能够 移动 的 最大 次数。
     */
    int[][] direction={{-1,1},{0,1},{1,1}};
    int MaxStep=Integer.MIN_VALUE;
    public int maxMoves(int[][] grid) {
        for(int i=0;i<grid.length;i++){
            myfun(grid,i,0,0);
        }
        return MaxStep;
    }
    public void myfun(int[][] grid,int row,int col,int step){
        if(step>=grid[0].length){
            MaxStep=Math.max(step,MaxStep);
            return;
        }
        boolean flag=false;
        for(int i=0;i<direction.length;i++){
            if(row+direction[i][0]<grid.length &&col+direction[i][1]<grid[0].length && row+direction[i][0]>=0 &&row+direction[i][1]>=0 &&   grid[row][col]<grid[row+direction[i][0]][col+direction[i][1]]){
                flag=true;
                myfun(grid,row+direction[i][0],col+direction[i][1],step+1);
            }
        }
        if(!flag){
            MaxStep=Math.max(step,MaxStep);
        }
    }
}

带有记忆化搜索的递归:

java 复制代码
class Solution {
    int m;
    int n;
    int[][] direction = new int[][]{{-1, 1}, {0, 1}, {1, 1}};
    public int maxMoves(int[][] grid) {
        m = grid.length;
        n = grid[0].length;

        int[][] dis = new int[m][n];
        for(int i = 0; i < m; i++){
            Arrays.fill(dis[i], -1);
        }

        int result = 0;

        for(int i = 0; i < m; i++){
            result = Math.max(result, findPath(i, 0, grid, dis) );
        }
        return result;
    }

    private int findPath(int x, int y, int[][] grid, int[][] dis){
        if(dis[x][y] != -1){
            return dis[x][y];
        }

        int maxLength = 0;
        for(int i = 0; i < 3; i++){
            int new_x = x + direction[i][0];
            int new_y = y + direction[i][1];
            if(new_x >= 0 && new_x < m && new_y < n && grid[new_x][new_y] > grid[x][y]){
                maxLength = Math.max(maxLength, findPath(new_x, new_y, grid, dis) + 1);
            }
        }

        dis[x][y] = maxLength;
        return dis[x][y];
    }
}
相关推荐
-dzk-40 分钟前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
TracyCoder1232 小时前
LeetCode Hot100(19/100)——206. 反转链表
算法·leetcode
踩坑记录3 小时前
leetcode hot100 94. 二叉树的中序遍历 easy 递归 dfs
leetcode
醉颜凉5 小时前
【LeetCode】打家劫舍III
c语言·算法·leetcode·树 深度优先搜索·动态规划 二叉树
达文汐5 小时前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
一匹电信狗5 小时前
【LeetCode_21】合并两个有序链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl
User_芊芊君子5 小时前
【LeetCode经典题解】搞定二叉树最近公共祖先:递归法+栈存路径法,附代码实现
算法·leetcode·职场和发展
培风图南以星河揽胜5 小时前
Java版LeetCode热题100之零钱兑换:动态规划经典问题深度解析
java·leetcode·动态规划
算法_小学生5 小时前
LeetCode 热题 100(分享最简单易懂的Python代码!)
python·算法·leetcode
执着2595 小时前
力扣hot100 - 234、回文链表
算法·leetcode·链表