题目

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:

nums = [
[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.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Show Tags

题目大意:给出一个矩阵,求矩阵中最大增序列的长度。

简单的思路

这道题可以看作一个四叉树的深度优先搜索

为什么题目中的矩阵可以看作四叉树?因为两个相邻元素,只有大小不相等时,才会有一条边,从大的元素指向小的元素。这样连接之后不可能出现环,因为若有环存在,说明有a>b>…>a,显然不成立。连接完所有的边之后,以最小元素为根,上下左右四个邻居为子结点,形成了一颗四叉树。

想要求最大递增序列,就是从根节点到最深的叶子结点,也就是求这棵四叉树的深度。思路是遍历矩阵的元素,对于每一个元素,求出其相邻且比它更大的元素的深度,取其最大值加一后作为自己的深度。

采用普通的深度优先算法,需要递归调用,效率比较低。

递归过程:

如果该元素相邻元素可以访问(在边界内、比当前元素大),就依次求出邻居的深度,取最大值加一作为自己的深度。递归出口:没有可访问邻居,深度为1(只有自己)。

改进的思路

增加一个memo矩阵,作为记录。对于没有访问过的元素,置其值为0,代表没有访问过。对于访问的元素,求出当前元素的深度,存在memo中。

代码如下:

class Solution {
public:
int longestIncreasingPath(vector<vector<int>>& matrix) {
if (matrix.size() == )
return ;
int width = matrix[].size();
int height = matrix.size();
int result = ;
vector<vector<int>> memo(height, vector<int>(width, ));
for (int i = ; i < height; i++)
for (int j = ; j < width; j++)
{
int tmp = nprocess(matrix, i, j, height, width, memo);
if (tmp > result)
result=tmp;
}
return result;
} int nprocess(vector<vector<int>> &matrix, int i, int j, int m, int n, vector<vector<int>> &memo)
{
if (memo[i][j] != ) return memo[i][j];
memo[i][j] = ;
vector<vector<int>> shift{ { , - }, { , }, { -, }, { , } };
int max = ;
for (int k = ; k < ; k++)
{
int x = i + shift[k][];
int y = j + shift[k][];
if ((x < m) && (x >= ) && (y<n) && (y >= ) && (matrix[x][y]>matrix[i][j]))
{
int tmp = nprocess(matrix, x, y, m, n, memo) + ;
if (tmp > max)
max = tmp;
}
}
memo[i][j] = max;
return max;
}
};

总结

重点是对于递归过程的优化。

一个问题是,将矩阵转换成四叉树。我一开始的想法,是将矩阵转换成有向图,考虑到可能存在的环路i,在深度遍历中加入了一个矩阵表示该结点是否被访问过。事实是,这个矩阵不可能是有环路的有向图(不存在a>b>…>a),这样就退化成了一棵四叉树。所以,因为没有环路的存在,是否被访问过的结点在未加入记忆矩阵的深度优先中没有作用。

LeetCode #329. Longest Increasing Path in a Matrix的更多相关文章

  1. leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)

    https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, find the ...

  2. [LeetCode] 329. Longest Increasing Path in a Matrix ☆☆☆

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  3. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question

    在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.

  4. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  5. 329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径

    Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...

  6. [LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  7. 329. Longest Increasing Path in a Matrix(核心在于缓存遍历过程中的中间结果)

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  8. 329. Longest Increasing Path in a Matrix

    最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...

  9. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

随机推荐

  1. 应用服务器和Web服务器

    如上图所示,绝大部分的公司会采用Apache+tomcat集群(或jetty集群)来部署公司的Web服务, Web服务器和应用服务器关系,先介绍一下我们常说的服务器: Tomcat服务器,是运行ser ...

  2. Windows Git中文文件名乱码

    在Windows下使用git,安装包如下: https://git-for-windows.github.io/ 在使用git bash时git 默认中文文件名是 xx% 是因为 对0x80以上的字符 ...

  3. Java final数据

    许多程序设计语言都有自己的办法告诉编译器某个数据是"常数".常数主要应用于下述两个方面: 1)编译期常数,它永远不会改变: 2)在运行期间初始化的一个值,我们不希望它发生变化.ss ...

  4. SequenceInputStream

    SequenceInputStream从名字上看, 他是一个序列字节输入流 既然是个序列 那么意味着 SequenceInputStream装着许多的输入流 所以 可以用他来合并文件 Sequence ...

  5. 20款时尚的 WordPress 博客主题【免费下载】

    在这篇文章中,我们收集了20款时尚的 WordPress 博客模板.WordPress 作为最流行的博客系统,插件众多,易于扩充功能.安装和使用都非常方便,而且有许多第三方开发的免费模板,安装方式简单 ...

  6. ae工具是一种特殊的命令

    itool继承icommand,所以itool工具的调用类似于icommand,而icommand的调用主要是oncreate和onclick方法,oncreate需要传入事件执行的的对象,oncli ...

  7. 如何排查sharepoint2010用户配置文件同步服务启动问题

    用户配置文件同步服务与 Microsoft Forefront Identity Manager (FIM) 交互,以与外部系统(如目录服务和业务系统)同步配置文件信息.启用用户配置文件同步服务时,将 ...

  8. SharePoint 2013 配置基于AD的Form认证

    前 言 配置SharePoint 2013基于AD的Form认证,主要有三步: 1. 修改管理中心的web.config: 2. 修改STS Application的web.config: 3. 修改 ...

  9. SharePoint 2013 关于自定义显示列表表单的bug

    1.在SharePoint 2013中,我们隐藏列表Dispform页面的ListFormWebPart部件,转而使用自定义显示列表表单进行展示,因为这样更容易定制我们需要的显示: 2.之后发现文件夹 ...

  10. JS常用的function集合

    1.把字符串转为日期格式  (1) var str ='2012-08-12 23:13:15';str = str.replace(/-/g,"/");var date = ne ...