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 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.
class Solution {
public:
bool checkRange(vector<vector<int> >& matrix, int x, int y) {
int n = matrix.size(), m = matrix[].size();
if(x < || y < || x >= n || y >= m) return false;
return true;
}
int dfs(vector<vector<int> >& matrix, vector<vector<int> >& dp, vector<vector<bool> >& vis, vector<vector<int> >& dir, int x, int y) {
if(dp[x][y]) return dp[x][y];
int MAX = -;
for(int i=; i<dir.size(); ++i) {
int nx = x + dir[i][], ny = y + dir[i][];
if(checkRange(matrix, nx, ny) && !vis[nx][ny] && matrix[nx][ny] > matrix[x][y]) {
vis[nx][ny] = true;
MAX = max(MAX, dfs(matrix, dp, vis, dir, nx, ny) + );
vis[nx][ny] = false;
}
}
if(MAX == -) return ;
dp[x][y] = MAX;
return dp[x][y];
}
int longestIncreasingPath(vector<vector<int>>& matrix) {
int n = matrix.size();
if(n == ) return ;
int m = matrix[].size();
vector<vector<int> > dp(n, vector<int>(m, ));
vector<vector<bool> > vis(n, vector<bool>(m, false));
vector<vector<int> > dir();
dir[].push_back(-); dir[].push_back();
dir[].push_back(); dir[].push_back();
dir[].push_back(); dir[].push_back();
dir[].push_back(); dir[].push_back(-);
int res = -;
for(int i=; i<n; ++i) {
for(int j=; j<m; ++j) {
vis[i][j] = true;
dp[i][j] = dfs(matrix, dp, vis, dir, i, j);
res = max(res, dp[i][j]);
vis[i][j] = false;
}
}
return res;
}
};
leetcode@ [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)的更多相关文章
- 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 ...
- [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]表示以此点开始的最长路径,这样每次碰 ...
- Java实现 LeetCode 688 “马”在棋盘上的概率(DFS+记忆化搜索)
688. "马"在棋盘上的概率 已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有 ...
随机推荐
- pthread_create用法
linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...
- NYOJ 题目15 括号匹配(二)(区间DP)
点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...
- Qt读写二进制文件
http://blog.csdn.net/mjlsuccess/article/details/22194653 http://www.cnblogs.com/weiweiqiao99/archive ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-009-拦截请求()
一. 对特定的请求拦截 For example, consider the requests served by the Spittr application. Certainly, thehome ...
- Stop-The-World
Stop-The-World –Java中一种全局暂停的现象 –全局停顿,所有Java代码停止,native代码可以执行,但不能和JVM交互 –多半由于GC引起 •Dump线程 •死锁检查 •堆Dum ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- CentOS 命令随笔
linux下敲命令时:快速删除当前行已经敲的命令: CTR+U 或者 CTR+/ 快速删除当前行刚输入接近鼠标当前位置的单词:CTR+W 以上在XS ...
- 新LNMP环境,但是SESSION跨页面或者刷新 session丢失
唠叨:本地WAMP做了一个微信项目,一切OK,昨天需要在LINUX架设一套LNMP环境.但是,在架设代码的时候,登录总是不成功~~~ 最终解决:权限 其实,作为一个程序猿,只要涉及到服务器内容,都应该 ...
- android系统平台显示驱动开发简要:LCD基本原理篇『一』
平台信息:内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博客 ...
- Android 签名(7)签名常见问题,debug签名和release签名的区别等
一般在安装时提示出错:INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES 1) 两个应用,名字相同,签名不同 2) 升级时前一版本签名,后一版本没签名 3) ...