[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 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:
Input: nums =
[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4
Explanation: The longest increasing path is[1, 2, 6, 9]
.
Example 2:
Input: nums =
[
[3,4,5],
[3,2,6],
[2,2,1]
]
Output: 4
Explanation: The longest increasing path is[3, 4, 5, 6]
. Moving diagonally is not allowed.
这个题目我理解为还是Dynamic Programming, 虽然说是加入了memoization, 但是DP的本质不本来也是memoization么? anyways, 这个题目就是用DFS, 但是每次的结果我们会存
在dp数组里面, 另外有个visited set, 去标记我们是否已经visited过了, 已经得到这个元素的值, 如果是的话直接返回, 节省time,
另外 function的话就是A[i][j] = max(A[i][j], helper(neig) + 1), 需要注意的是dp数组存的是以该点作为结束的点的最大值, 那么我们就需要向小的值去search, 所以有
matrix[i][j] > matrix[nr][nc] 的判断在那里.
1. Constraints
1) empty , return 0
2) element will be interger, 有duplicates, 但是increasing 是绝对的, 所以不用担心duplicates
2. Ideas
memoization DFS, T: O(m*n) S: O(m*n)
1) edge case
2) helper dfs function, 向4个邻居方向search, max(A[i][j], helper(neig) + 1), 最开始的时候加判断, 如果flag, 直接返回dp[i][j]
3) for lr, for lc, ans = max(ans, helper(i,j))
4) return ans
3. code
class Solution:
def longestIncreasePath(self, matrix):
if not matrix or not matrix[0]: return 0
lrc, ans, dirs = [len(matrix), len(matrix[0])], 0, [(1,0), (-1, 0), (0,1), (0,-1)]
dp, visited = [[1]*lrc[1] for _ in range(lrc[0])] , set()
def helper(i, j):
if (i, j) in visited:
return dp[i][j]
visited.add((i, j))
for c1, c2 in dirs:
nr, nc = c1 + i, c2 + j
if 0 <= nr < lrc[0] and 0 <= nc < lrc[1] and matrix[i][j] > matrix[nr][nc]:
dp[i][j] = max(dp[i][j], helper(nr, nc) + 1)
return dp[i][j]
for i in range(lrc[0]):
for j in range(lrc[1]):
ans = max(ans, helper(i,j))
return ans
4. Test cases
[
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4
[LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization的更多相关文章
- 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 ...
- [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 ...
- 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) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
随机推荐
- B - Image Perimeters
Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected f ...
- html表格的基本用法
表格的基本用法 1.<!DOCTYPE html><html><head lang="en"> <meta charset="U ...
- 极验(geetest)验证码
最近在做项目的时候,需要用到登录验证,在网上看到了一个很不错的验证插件,在此记录一下使用流程. 极限验证码 官网:http://www.geetest.com/,到GitHub下载服务端代码htt ...
- 使用Zabbix监控RabbitMQ消息队列
参考文档:http://blog.51cto.com/270142877/1937241 本项目脚本下载地址:https://github.com/jasonmcintosh/rabbitmq-zab ...
- poj3080 Blue Jeans【KMP】【暴力】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions:21746 Accepted: 9653 Descri ...
- 架构师如何借鉴他人经验快速成长? | 2018GIAC上海站日程上线!
随着网络技术的迅猛发展,越来越多的企业需要紧跟技术发展潮流以应对层出不穷的业务场景变化.如今多“语言”开发百花齐放,选择何种语言才能在合适的场景中发挥最大价值?互联网业务架构经过了长年的发展,已然朝着 ...
- 【基本功】Java动态追踪技术探究 不重启JVM,替换掉已经加载的类?不重启JVM,获知运行时对象的属性
https://mp.weixin.qq.com/s/_hSaI5yMvPTWxvFgl-UItA 小结: 1.根据Java的类加载机制,在同一个ClassLoader中,类是不允许重复的: 2.单例 ...
- 对内存分配的理解 自动变量 局部变量 临时变量 外部变量 字符串长度 C语言可以看成由一些列的外部对象构成
Status ListInsert_Sq(SqList *L,int i,LElemType_Sq e) { LElemType_Sq *newbase; LElemType_Sq *p,*q; if ...
- 【Linux】Linux 常用命令汇总
查看软件xxx安装内容:dpkg -L xxx 查找软件库中的软件:apt-cache search 正则表达式 查找软件库中的软件:aptitude search 软件包 查找文件属于哪个包:dpk ...
- JavaScript学习笔记--语言工具的了解
基础学习,快速入门资料:网站 https://www.liaoxuefeng.com ,http://www.runoob.com/js/js-tutorial.html 笔记: 编程工具:SubLi ...