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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question

    在递归调用的函数中使用了max = INT_MIN,结果报超时错误,改为max=0就对了,虽然在这题中最小就为0, 看来在之后最小为0的时候,就不要使用INT_MIN了.

  5. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  6. 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 ...

  7. 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 ...

  8. 329. Longest Increasing Path in a Matrix

    最后更新 三刷? 找矩阵里的最长路径. 看起来是DFS,实际上也就是.但是如果从每个点都进行一次DFS然后保留最大的话,会超时. 这里需要结合DP,dp[i][j]表示以此点开始的最长路径,这样每次碰 ...

  9. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

随机推荐

  1. JVM源码分析之栈溢出完全解读

    概述 之所以想写这篇文章,其实是因为最近有不少系统出现了栈溢出导致进程crash的问题,并且很隐蔽,根本原因还得借助coredump才能分析出来,于是想从JVM实现的角度来全面分析下栈溢出的这类问题, ...

  2. react-snippets

    rcjc class componentName extends Component { render() { return ( <div> </div> ); } } con ...

  3. day10 十 函数、形参和实参

    一.形参和实参 1.形参:在函数定义()中出现的参数形参就是拷贝实参的值,随着函数的调用才产生,随着函数调用结束而销毁 def fn(a, b, c): print(a) print(b) print ...

  4. The Salt Master has cached the public key报错解决办法

    参考:http://www.52devops.com/chuck/814.html 查看salt-minion的运行状态,显示salt-master已经缓存了这个minion,但是minion在重新认 ...

  5. DB2 性能分析工具介绍:Event Monitor 篇(转)

    https://www.ibm.com/developerworks/cn/data/library/techarticle/dm-1112qiaob/ 引言 DB2 提供了两个比较常用的数据库性能分 ...

  6. vins-mono源码解读

    https://blog.csdn.net/q597967420/article/details/76099409

  7. css中display:inline-block display:-moz-inline-box display:-moz-inline-stack 的区别

    很多时候我们必须使一些块元素并排显示,一般想到的是必须使用浮动,但是块元素浮动给边距(margin)的时候在IE下会出现加倍的BUG,所以很多时候不得不把这个块元素套在一个内联元素里面,然后给内联元素 ...

  8. shell脚本之流程控制语句

    一.分支控制语句 1.if .. fi条件 if condition; then action fi 2.if .. else .. fi条件 if condition;then action; el ...

  9. MVC模型和MVT模型

    MVC 大部分开发平台都需要搭建的后台框架,Java和PHP最为喜爱 M:model.     模型.      主要用于对数据库层的封装 V:view.        视图.      用于对用户展 ...

  10. PHP之类型转化

    类型转化的判别 PHP在变量定义中不需要(或者不支持)明确的类型定义:变量类型是根据使用该变量的上下文所决定的, 也就是说,如果把一个string值付给变量$var,$var就成了一个string,如 ...