[leetcode-329]Longest Increasing Path in a Matrix(java)

原题:这里写链接内容

分析:这道题很容易想到DFS的做法,于是写成了代码1,但是TLE,其实也很正常,因为在遍历过程中可能多次经历了某些中间状态。而这些状态实际上只需要访问一次就可以了,于是又写出了代码2,DFS+动态规划。

第一版:

public class Solution {
    int[][] matrix;
    int matrixRow;
    int matrixCol;
    public int longestIncreasingPath(int[][] matrix) {
        this.matrix = matrix;
        int maxCount = 0;
        matrixRow = matrix.length;
        if(matrixRow == 0)
            return 0;
        matrixCol = matrix[0].length;

        for(int row = 0;row<matrixRow;row++){
            for(int col = 0;col<matrixCol;col++){
                int count = dfs(row,col,1);
                if(count > maxCount)
                    maxCount = count;
            }
        }
        return maxCount;
    }
    private int dfs(int row,int col,int count){
        int tmpCount = 0;
        int[] rows = {-1,1,0,0};
        int[] cols = {0,0,-1,1};

        for(int i = 0;i<rows.length;i++){
            int currentRow = row+rows[i];
            int currentCol = col+cols[i];
            if(currentRow<0 || currentRow>=matrixRow)
                continue;
            if(currentCol<0 || currentCol>=matrixCol)
                continue;
            if(matrix[currentRow][currentCol] > matrix[row][col])
                tmpCount = Math.max(tmpCount,dfs(currentRow,currentCol,count+1));
        }
        return Math.max(count,tmpCount);

    }
}

第二版

public class Solution {
    int[][] matrix;
    int[][] countMatrix;
    int matrixRow;
    int matrixCol;

    int res = 0;
    public int longestIncreasingPath(int[][] matrix) {
        this.matrix = matrix;
        matrixRow = matrix.length;
        if(matrixRow <= 0)
            return 0;
        matrixCol = matrix[0].length;
        countMatrix = new int[matrixRow][matrixCol];
        initCountMatrix();

        for(int i = 0;i<matrixRow;i++){
            for(int j = 0;j<matrixCol;j++){
                dfs(i,j);
            }
        }
        return res;
    }
    private void dfs(int row,int col){
        int[] rows = {-1,1,0,0};
        int[] cols = {0,0,-1,1};

        int count  = 1;
        for(int i = 0;i<4;i++){
            int tmpRow = row+rows[i];
            int tmpCol = col+cols[i];

            if(tmpRow < 0 || tmpRow >= matrixRow)
                continue;
            if(tmpCol < 0 || tmpCol >= matrixCol)
                continue;

            if(matrix[tmpRow][tmpCol] > matrix[row][col]){
                if(countMatrix[tmpRow][tmpCol] == -1){
                    dfs(tmpRow,tmpCol);
                }
                count = Math.max(count,countMatrix[tmpRow][tmpCol]+1);
            }
        }
        countMatrix[row][col] = count;
        res = Math.max(res,count);
    }
    private void initCountMatrix(){
        for(int i = 0;i<matrixRow;i++){
            for(int j = 0;j<matrixCol;j++)
                countMatrix[i][j] = -1;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值