Java实现 LeetCode 329 矩阵中的最长递增路径

329. 矩阵中的最长递增路径

给定一个整数矩阵,找出最长递增路径的长度。

对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。

示例 1:

输入: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
输出: 4
解释: 最长递增路径为 [1, 2, 6, 9]。
示例 2:

输入: nums =
[
[3,4,5],
[3,2,6],
[2,2,1]
]
输出: 4
解释: 最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。

class Solution {
     public int longestIncreasingPath(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int max = 0;
        int row = matrix.length;
        int col = matrix[0].length;
        int[][] dp = new int[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                max = Math.max(max, loop(matrix, Integer.MIN_VALUE, dp, i, j)); //从每个节点开始搜索
            }
        }
        return max;
    }
    /**
     * 记忆化搜索
     * @param mat  数据矩阵
     * @param pre  路径中的前一个结点数字
     * @param dp   保存从每个结点开始搜索的最长升序序列的长度
     * @param i    当前位置
     * @param j    当前位置
     * @return
     */
    private int loop(int[][] mat, int pre, int[][] dp, int i, int j) {
        if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] <= pre) {  //停止搜索条件
            return 0;
        }
        if (dp[i][j] != 0) {  //如果有数据,直接返回
            return dp[i][j];
        }
        int max = 0;  //进行搜索
        max = Math.max(max, loop(mat, mat[i][j], dp, i - 1, j));
        max = Math.max(max, loop(mat, mat[i][j], dp, i + 1, j));
        max = Math.max(max, loop(mat, mat[i][j], dp, i, j - 1));
        max = Math.max(max, loop(mat, mat[i][j], dp, i, j + 1));
        dp[i][j] = max + 1;
        return dp[i][j];
    }
}
发布了1443 篇原创文章 · 获赞 1万+ · 访问量 167万+

猜你喜欢

转载自blog.csdn.net/a1439775520/article/details/104718629