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.

int dx[] = { 1 , -1, 0 , 0  };
int dy[] = { 0 , 0 , 1 , -1 };
class Solution {
public:
int dfs(int x, int y, const int &m,const int &n,vector<vector<int>>& matrix, vector<vector<int>>& dis) {
if (dis[x][y]) return dis[x][y]; for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < m && ny < n && matrix[nx][ny] > matrix[x][y]) {
dis[x][y] = max(dis[x][y], dfs(nx, ny, m, n, matrix, dis));
}
}
return ++dis[x][y];
} int longestIncreasingPath(vector<vector<int>>& matrix) {
if (!matrix.size()) return 0;
int m = matrix.size(), n = matrix[0].size();
vector<vector<int> > dis(m, vector<int>(n, 0));
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ans = max(ans, dfs( i, j, m, n, matrix, dis));
}
}
return ans;
}
};

最新文章

  1. IE10开始支持CSS3 Transitions, Transforms 和 Animations
  2. python学习道路(day8note)(抽象类,类的方法,异常处理,socket编程)
  3. PHP多文件上传(二维数组$_FILES(&#39;文件域的名称&#39;),move_uploaded_file(‘临时文件名’,‘新的文件名’))
  4. Oracle primary,unique,foreign 区别,Hibernate 关联映射
  5. windows程序防狼术入门
  6. AJAX初步
  7. ios开发——实战Swift篇&amp;简单项目的实现
  8. [置顶] 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
  9. 总结的git操作命令小抄集
  10. OpenCV探索之路(十):图像修复技术
  11. 使用JDK自带的MessageDigest计算消息摘要
  12. Android开发技巧——自定义控件之增加状态
  13. golang实现文字云算法
  14. SLAM+语音机器人DIY系列:(八)高阶拓展——2.centos7下部署Django(nginx+uwsgi+django+python3)
  15. “行业客户云原生最佳实践日” 亮相KubeCon上海
  16. 【原创】单片系统SoC
  17. Linux IPC之共享内存
  18. Mysql(压缩包)下载与安装
  19. ng-深度学习-课程笔记-4: 浅层神经网络(Week3)
  20. iOS-Apple苹果iPhone开发公开API

热门文章

  1. shell 脚本 测试webApp
  2. 第三章 RNA测序
  3. 在Excel中根据某一个单元格的出生日期自动精确计算年龄
  4. SqlServer和MySql允许脏读的实现方式,提高查询效率
  5. 8.19 extjs jar 包使用。
  6. springmvc cfx 整合
  7. js实现a_b变成A B的两种方法
  8. 2018.09.27 bzoj2510: 弱题(概率dp+循环矩阵优化)
  9. 2018.09.23 codeforces 1053A. In Search of an Easy Problem(gcd)
  10. 2018.06.30 cdq分治