Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【每日一题】:LeetCode 329. Longest Increasing Path in a Matrix #5

Open
watchpoints opened this issue Aug 20, 2019 · 4 comments
Assignees
Labels
Medium success 已经解决

Comments

@watchpoints
Copy link
Contributor

watchpoints commented Aug 20, 2019

Given an integer matrix, find the length of the longest increasing path

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

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

示例 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]。注意不允许在对角线方向上移动。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
@watchpoints
Copy link
Contributor Author

必须四面都返回才可以

@watchpoints
Copy link
Contributor Author

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

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

示例 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]。注意不允许在对角线方向上移动。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

直觉:
   2个方向移动,和4个方向移动 问题复杂度就增加了。具体增加在哪里的不清楚
想法:
最长递增路径的长度,四个方向,每个点都可能,2个方向不一样。

描述:

**/
class Solution {
public:
    int longestIncreasingPath(vector<vector<int>>& matrix) {
     int rows = matrix.size();
     if (0 == rows)
     {
         return 0;
     }
        
     int cols = matrix[0].size();
     vector<vector<int>> dp (rows,vector<int>(cols,0));//start from (i,j)
     int longest =0;
     
     for (int i=0;i < rows;i++)
     {
        for (int j=0;j < cols ;j++)
        {    
            //没有被访问过
             if ( 0 == dp[i][j])
             {
                 int temp =dfs(matrix,INT_MIN,i,j,dp,rows,cols);//计算一个节点
                 longest =max(temp,longest);
             }
        }
     }
        
    return longest;
  }
  
  int dfs(vector<vector<int>>& matrix,int pre,int i,int j,vector<vector<int>>& dp,int rows,int cols )
  {
      if ( i < 0 || j <0 || i >=rows || j>=cols)
      {
          return 0;
      }
      
      if ( pre >= matrix[i][j])
      {
          return 0; //这个方向是降序
      }
      
      if ( dp[i][j] > 0)
      {
          return dp[i][j]; //已经存在
      }
      //依赖关系:这个不是从上到下或者从下到上关系。每个节点都为其他人服务
      int left  =dfs(matrix,matrix[i][j],i,j-1,dp,rows,cols);
      int right =dfs(matrix,matrix[i][j],i,j+1,dp,rows,cols);
      int up    =dfs(matrix,matrix[i][j],i+1,j,dp,rows,cols);
      int down  =dfs(matrix,matrix[i][j],i-1,j,dp,rows,cols);
      
      dp[i][j]=max(max(left,right),max(up,down))+1;
      
      return dp[i][j];
      
  }
};

@watchpoints
Copy link
Contributor Author

第二次刷题 忘记 dp[i][j]=max(max(left,right),max(up,down))+1; +1操作

@watchpoints watchpoints added the success 已经解决 label Oct 22, 2019
@watchpoints watchpoints self-assigned this Oct 22, 2019
@watchpoints
Copy link
Contributor Author

watchpoints commented Apr 27, 2020

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Medium success 已经解决
Projects
None yet
Development

No branches or pull requests

1 participant