[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) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
随机推荐
- JVM源码分析之栈溢出完全解读
概述 之所以想写这篇文章,其实是因为最近有不少系统出现了栈溢出导致进程crash的问题,并且很隐蔽,根本原因还得借助coredump才能分析出来,于是想从JVM实现的角度来全面分析下栈溢出的这类问题, ...
- 一块移动硬盘怎样兼容Mac和Windows系统,并且可以在time machine上使用
Mac的内存通常不是很大,加上使用Mac的time machine备份的话,是需要外置U盘或硬盘的.U盘存储空间较小,大家也不会去分区,直接拿来用在Mac上或者Windows上就可以了.所以这里只说硬 ...
- html学习_表格、表单
表格(table):是用来处理表格式数据的,不是用来布局的. table > tr(行标签)> td(单元格标签) 1.表格注意事项: tr只能放置td标签,td里面可以放置任意元素. ...
- CSS3 transform 属性
CSS3 transform 属性 语法: transform: none|transform-functions; 值 描述 none 定义不进行转换. matrix(n,n,n,n,n,n) 定义 ...
- 一道hive SQL面试题
一.hive中实现方法 基表: 组表: gt gid gname 1001 g1 1002 g2 1003 g3 create table g( gid int, gname string )ro ...
- HTML响应状态码
https://www.restapitutorial.com/httpstatuscodes.html
- azkaban---visualize crontab--frontail
azkaban---visualize crontab azkaban--docker-----http://www.jkeabc.com/254015.html azkaban--tips ht ...
- UI自动化遇到的问题
1.找不到元素:查看是否有[frame]或者[iframe]元素 2.click无响应:1)使用sendkeys(Keys.Enter) 还是无响应:使用js定位 3.弹框处理: 确认:switcht ...
- falsk 与 django 钩子方法
falsk 四大钩子方法# 在第一次请求之前调用@app.before_first_requestdef before_first_request(): print("这是第一次请求之前调用 ...
- SQL server 2005数据库的还原与备份
一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2005→SQL Server Management Studio ,这里我以UMVTEST命 ...