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 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的更多相关文章
- 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 ...
- [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 ...
- [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 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 ...
- [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 ...
- 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 ...
- 329. Longest Increasing Path in a Matrix
最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
随机推荐
- Javascript——闭包、作用域链
1.闭包:是指有权访问另一个函数作用域中的变量的函数.创建闭包的常见方式:在一个函数内部创建另一个函数. function f(name){ return function(object){ var ...
- python补充最常见的内置函数
最常见的内置函数是: print("Hello World!") 数学运算 abs(-5) # 取绝对值,也就是5 round(2. ...
- rabbitmq心跳机制与配置
最近,我们有些在阿里云上的应用总是有客户端出现异常和信息推送不及时的情况,检查mq日志,发现高峰期不停的有心跳超时,如下: =ERROR REPORT==== 21-Dec-2016::12:38:0 ...
- 64位系统使用Access 数据库文件的彻底解决方法
最近,有PDF.NET用户问我怎么在64位系统下无法访问Access数据库的问题,我第一反应是我怎么没有遇到呢?今天一看自己的VS和Office都是32位版本的,所以在VS里面调试访问Access是没 ...
- 设计模式——Spring IoC中用到的模板方法模式
基本概念 什么是模板方法(Template method):父类定义了骨架(调用哪些方法及顺序),某些特定方法由子类实现. 最大的好处:代码复用,减少重复代码.除了子类要实现的特定方法,其他方法及方法 ...
- Apple Watch PSD 源文件【免费素材下载】
Apple Watch 是苹果公司于2014年9月发布的一款智能手表.分为运动款.普通款和定制款三种,采用蓝宝石屏幕,有银色,金色,红色,绿色和白色等多种颜色可以选择.这里分享的是 Apple Wat ...
- 提高CSS文件可维护性的五种方法
当完成一项前端的工作之后,许多人都会忘记该项目的结构与细节.然而代码并不是马上就能完全定型,在余下的时间里还有不断的维护工作,而这些工作也许不会是你自己完成.所以,结构优良的代码能很大程度上优化它的可 ...
- SharePoint 2013 使用 PowerShell 更新用户
在SharePoint开发中,经常会遇到网站部署,然而,当我们从开发环境,部署到正式环境以后,尤其是备份还原,所有用户组的用户,还依然是开发环境的,这时,我们就需要用PowerShell更新一下: P ...
- 转-Nmap扫描原理与用法
1 Nmap介绍 操作系统与设备类型等信息. Nmap的优点: 1. 灵活.支持数十种不同的扫描方式,支持多种目标对象的扫描. 2. 强大.Nmap可以用于扫描互联网上大规 ...
- 虚拟机克隆以后出现“需要整合虚拟机磁盘”的解决方法
问题描述 在虚拟机克隆完毕以后,原始虚拟机提示"需要整合虚拟机磁盘" 在"任务与事件"栏中看到以下信息 解决方法 从上面可以看到是因为整合失败导致的,那么我们只 ...