矩阵中递归最长增长路径

时间:2019-11-09 23:35:22

标签: java recursion depth-first-search

我正在实现leetcode的最长路径增长问题。

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

在每个单元格中,您可以向四个方向移动:向左,向右,向上或向下。您不得对角移动或移动到边界之外(即不允许环绕)。

示例1:

输入:nums = [   [9,9,4],   [6,6,8],   [2,1,1] ] 输出4 说明:最长的增长路径是[1、2、6、9]。

因此,以下是我的实现在递归上进行了大量尝试,但无法理解为什么在此示例中maxDist从4减少到3到2时为什么没有给出正确的结果,因为此变量是全局变量而不是局部变量。 >

public class LongestIncreasingPath {

    private static final int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
    private int m, n;
    int maxDist;

    public int longestIncreasingPath(int[][] matrix) {
        if (matrix.length == 0)
            return 0;
        m = matrix.length;
        n = matrix[0].length;
        int ans = 1;
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j) {
                dfs(matrix, i, j, 1);
                ans = Math.max(ans, maxDist);
            }
        return ans;
    }
    private int dfs(int[][] matrix, int i, int j, int dist) {
        for (int[] d : dirs) {
            int x = i + d[0], y = j + d[1];
            if (0 <= x && x < m && 0 <= y && y < n && matrix[x][y] > matrix[i][j]) {
                maxDist = Math.max(maxDist, dfs(matrix, x, y, dist+1));
            }
        }
        return dist;
    }

    public static void main(String[] args) {
        int[][] nums = { { 9, 9, 4 }, { 6, 6, 8 }, { 2, 1, 1 } };
        LongestIncreasingPath lIP = new LongestIncreasingPath();
        System.out.println(lIP.longestIncreasingPath(nums));
    }
}

1 个答案:

答案 0 :(得分:1)

以下是一个有效的版本,已在2个测试用例上进行了测试(仅)。请注意有关错误和结构更改的注释:

public class LongestIncreasingPath {

    private static final int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
    private int rows, cols;
    //avoid non-volatile class variable that may be updated by more than one thread
    //use local variables instead
    //private int maxDist;

    public int longestIncreasingPath(int[][] matrix) {
        if (matrix.length == 0) return 0;
        rows = matrix.length;
        cols = matrix[0].length;

        int maxDist = 0; //retain max found
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                //bug fix: use distance (matrix[row][col]) instead of 1
                int  distance = dfs(matrix, row, col, matrix[row][col]);
                maxDist = Math.max(distance, maxDist);
            }
        }
        return maxDist;
    }

    private int dfs(int[][] matrix, int row, int newCol, int dist) {

        int maxDist = dist; 
        for (int[]dir : dirs) {
            int newRow = row + dir[0], y = newCol + dir[1];
            if (0 <= newRow && newRow < rows && 0 <= y && y < cols &&
                                        matrix[newRow][y] > matrix[row][newCol]) {
                //bug fix: //add new distance matrix[x][y] instead of 1
                maxDist = Math.max(maxDist, dfs(matrix, newRow, y, dist + matrix[newRow][y]));
            }
        }
        return maxDist;
    }

    public static void main(String[] args) {

        LongestIncreasingPath lIP = new LongestIncreasingPath();
        int[][] nums = { { 9, 9, 4 },
                         { 6, 6, 8 },
                         { 2, 2, 1 }
                        };
        //printout test case 1
        System.out.println(lIP.longestIncreasingPath(nums));
        nums = new int[][]{ { 5, 6, 7 },
                            { 4, 9, 8 },
                            { 3, 2, 1 }
                        };
        //printout test case 2
        System.out.println(lIP.longestIncreasingPath(nums));
    }
}