Leetcode 329. Longest Increasing Path in a Matrix

329. Longest Increasing Path in a Matrix

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] .

Analysis: 

given the two-dimensional integer array, we need to find the Longest Increasing Path in a Matrix. we can move to four directions, up down right and left.

The first method is DFS, however it takes too much time, IF we use DFS, we need to start with all positions in this array and continue for the whole

array.

code:

DFS, I did not check the correcness of this method

public class Solution {
    int maxNum = Integer.MIN_VALUE;

    public int longestIncreasingPath(int[][] matrix) {
      int rowNum = matrix.length;
      if(rowNum == 0)
        return 0;
      int columnNum = matrix[0].length;
      if(column == 0)
        return 0;
      int row = 0;
      int col = 0;
      int number = 0;

      for(int i = 0; i < rowNum;i++){
        for(int j = 0; j < columnNum; j++){
          helper(i,j,matirx,0)
        }
      }
      return maxNum;
    }

    //This is the depth first search
    public int helper(int row, int col, int[][] matrix,int number){
      number++;
      if(number > maxNum )
        maxNum = number;
      // if(number == 1)
      // {
        // lastOne = matrix[row, col];
      int rowNum = matrix.length;
      int colNum = matrix[0].length;

      int temNum = number++;

      if(row - 1 >=0 && matrix[row - 1][col] > matrix[row][col]){
        int val = helper(row - 1,col,matrix,number);
        if(val > maxNum)
            maxNum = number;
        if(val > temNum)
          temNum = val;
      }

      if(row + 1 < rowNum  && matrix[row + 1][col] > matrix[row][col]){
        int val = helper(row + 1, col, matrix, number);
        if(val > maxNum)
            maxNum = number;
        if(val > temNum)
          temNum = val;
      }

      if(col - 1 >=0 && matrix[row][col - 1] > matrix[row][col]){
        int val = helper(row, col - 1, matrix, number);
        if(val > maxNum)
            maxNum = number;
        if(val > temNum)
          temNum = val;
      }

      if(col + 1 >=colNum && matrix[row][col + 1] > matrix[row][col]){
        int val = helper(row, col + 1, matrix,number);
        if(val > maxNum)
            maxNum = number;
        if(val > temNum)
          temNum = val;
      }

      return temNum;
    }
}
Dynamic programming + DFS.

public class Solution {
    int maxNum = Integer.MIN_VALUE;
    int[][] record;

    public int longestIncreasingPath(int[][] matrix) {
      int rowNum = matrix.length;
      if(rowNum == 0)
        return 0;
      int colNum = matrix[0].length;
      if(colNum == 0)
        return 0;
      record= new int[rowNum][colNum];
      //initialized the record array to all 0;
      for(int i = 0; i < rowNum; i++){
        for(int j = 0; j < colNum; j++){
          record[i][j] = 0;
        }
      }

      int row = 0;
      int col = 0;

      for(int i = 0; i < rowNum;i++){
        for(int j = 0; j < colNum; j++){
          helper(i,j,matrix);
        }
      }
      return maxNum;
    }

    //This is the depth first search
    public int helper(int row, int col, int[][] matrix){
      if(record[row][col] != 0)
        return record[row][col];

      // int number  = 1;
      // number++;
      // if(number > maxNum )
      //   maxNum = number;
      // if(number == 1)
      // {
        // lastOne = matrix[row, col];
      int rowNum = matrix.length;
      int colNum = matrix[0].length;

      int temNum = 1;

      if(row - 1 >=0 && matrix[row - 1][col] > matrix[row][col]){
        int val = 1 + helper(row - 1,col,matrix);
        if(val > maxNum)
            maxNum = val;
        if(val > temNum)
          temNum = val;
      }

      if(row + 1 < rowNum  && matrix[row + 1][col] > matrix[row][col]){
        int val = 1 + helper(row + 1, col, matrix);
        if(val > maxNum)
            maxNum = val;
        if(val > temNum)
          temNum = val;
      }

      if(col - 1 >=0 && matrix[row][col - 1] > matrix[row][col]){
        int val = 1 + helper(row, col - 1, matrix, number);
        if(val > maxNum)
          maxNum = val;
        if(val > temNum)
          temNum = val;
      }

      if(col + 1 < colNum && matrix[row][col + 1] > matrix[row][col]){
        int val = 1 + helper(row, col + 1, matrix,number);
        if(val > maxNum)
            maxNum = val;
        if(val > temNum)
          temNum = val;
      }

      if(temNum > maxNum )
        maxNum = temNum;

      record[row][col] = temNum;
      return temNum;
      // else{
    }
}


Lesson learned

I learn from this program that when do the dynamic programming, you need to consider whether or not to store the computation result in a array, so that you can avoid repeated computation. This may be called purnning.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值