LeetCode--329. Longest Increasing Path in a Matrix

Problem:

Given an integer matrix, find the length of the longest increasing path.
From each cell, you can either move to four directions: left, right,up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).

Example 1:
nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
] Return 4 The longest
increasing path is [1, 2, 6, 9].

Example 2:
[
[3,4,5],
[3,2,6],
[2,2,1]
] Return 4 The longest
increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.

Analysis:
题目大意是,给定一个整数矩阵,计算其要求元素排列是递增的,球最长递增路径的长度。
从任意一个矩阵位置出发,可向上下左右四个方向移动。不可以沿着对角线移动,也不能离开边界。(环绕也是不允许的)。题目还给出了两个测试用例。
解题思路是,深度优先搜索,但如果处理不好也可能超时,你需要加入记忆化搜索,具体方法如下:
从起点开始遍历矩阵,递归寻找其最长递增路径。定义辅助数组res ,用于记录已经搜索过的矩阵元素的数据,如res[x][y]存储了从坐标(x, y)出发的最长递增路径长度。之后,进行深度优先搜索。逐一检查某个元素的四个方向,并继续从所有可能出现最长路径的方向上进行搜索。 当遇到res[x][y]已算出的情况,直接返回res[x][y],减少运算量。

Answer:

public class Solution {
    //定义四个方向的x和y的坐标方向
    public int[] dx ={-1,0,1,0},dy={0,1,0,-1};
    public int longestIncreasingPath(int[][] matrix) {
        //判断特殊情况[]
        if(matrix.length==0 || matrix[0].length==0) return 0;
        int[][] res=new int[matrix.length][matrix[0].length];
        //给res初始化,均为0
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                res[i][j]=0;
            }
        }     
        //遍历每一个点   
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                dfs(i,j,matrix,res);
            }
        }
        //调用longes()函数,求嘴大长度
        return longest(res)+1;
    }

    public void dfs(int i,int j,int[][] matrix,int[][] res){
        for(int k=0;k<4;k++){
            int x = i+dx[k];
            int y = j+dy[k];
            if(x>=0 && x<matrix.length && y>=0 && y<matrix[0].length && matrix[i][j]<matrix[x][y] && res[x][y]<=res[i][j]){
                res[x][y]=res[i][j]+1;
                dfs(x,y,matrix,res);
            }
            /*上个if语句可以通过,这个通过不了(超时),主要是把多余的不符合res[x][y]<=res[i][j]的x,y又进行了一次dfs
             *if(x>=0 && x<matrix.length && y>=0 && y<matrix[0].length && matrix[i][j]<matrix[x][y]){
                res[x][y]=Math.max(res[x][y],res[i][j]+1);
                dfs(x,y,matrix,res);
            }
            */
        }
    }
    //遍历res求最长的链长
    public int longest(int[][] res){
        int longest=0;
        for(int i=0;i<res.length;i++){
            for(int j=0;j<res[0].length;j++){
                if(res[i][j]>longest) longest=res[i][j];
            }
        }
        return longest;
    }
}

比较正规的DFS+DP的解法(符合一般解DFS思路):

public class Solution{
    int[] dx = {-1, 1, 0, 0};
    int[] dy = {0, 0, -1, 1};
    public int longestIncreasingPath(int[][] matrix) {
        if( matrix.length == 0 )
            return 0;
        int rowLen = matrix.length;
        int colLen = matrix[0].length;
        int[][] dis = new int[rowLen][colLen]; // to store the longest increasing length from current point
        int res = 0;
        for(int i = 0; i < rowLen; i++){
            for(int j = 0; j < colLen; j++){
                res = Math.max(res, dfs(dis, rowLen, colLen, i, j, matrix));
            }
        }
        return res;
    }

    public int dfs(int[][] dis, int rowLen, int colLen, int x, int y, int[][] matrix) {
        //if already be visited, return
        if(dis[x][y] != 0)
            return dis[x][y]; 
        for(int i = 0; i < 4; i++)
        {   int newX = x + dx[i];
            int newY = y + dy[i];
            if(newX >= 0 && newY >= 0 && newX < rowLen && newY < colLen && matrix[x][y] < matrix[newX][newY])
                dis[x][y] = Math.max(dis[x][y], dfs(dis, rowLen, colLen, newX, newY, matrix));
        }
        dis[x][y]++;
        return dis[x][y];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值