【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
题目描述:
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.
题目大意
求二维矩阵中最长的递增路径。
解题方法
和417. Pacific Atlantic Water Flow非常类似,直接DFS求解。一般来说DFS需要有固定的起点,但是对于这个题,二维矩阵中的每个位置都算作起点。
把每个位置都当做起点,然后去做个dfs,看最长路径是多少。然后再找出全局的最长路径。使用cache保存已经访问过的位置,这样能节省了很多搜索的过程,然后有个continue是为了剪枝。因为这个做法比较暴力,就没有什么好讲的了。
最坏情况下的时间复杂度是O((MN)^2),空间复杂度是O(MN)。
class Solution(object):
def longestIncreasingPath(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: int
"""
if not matrix or not matrix[0]:
return 0
m, n = len(matrix), len(matrix[0])
res = 0
cache = [[-1] * n for _ in range(m)]
for i in range(m):
for j in range(n):
path = self.dfs(matrix, cache, m, n, i, j)
res = max(res, path)
return res
def dfs(self, matrix, cache, m, n, i, j):
if cache[i][j] != -1:
return cache[i][j]
directions = [(-1, 0), (1, 0), (0, 1), (0, -1)]
res = 1
for dire in directions:
x, y = i + dire[0], j + dire[1]
if x < 0 or x >= m or y < 0 or y >= n or matrix[x][y] <= matrix[i][j]:
continue
path = 1 + self.dfs(matrix, cache, m, n, x, y)
res = max(path, res)
cache[i][j] = res
return cache[i][j]
参考资料:
日期
2018 年 10 月 1 日 —— 欢度国庆!
【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)的更多相关文章
- 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了.
- 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]表示以此点开始的最长路径,这样每次碰 ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
随机推荐
- R包 tidyverse 分列
代码: 1 library(tidyverse) 2 separate(data = df,col=chr_pos,into=c("chr","pos"),se ...
- 01 Windows安装C语言环境
安装C语言运行环境 双击打开安装文件,进行安装 配置环境变量 将: C:\MinGW\bin;添加到Path变量里面. 验证环境变量是否成功 gcc –v 出现如下图所示,证明安装成功
- 巩固javaweb的第二十天
巩固内容: 同一个页面中的多个 form 在同一个页面中可以有多个 form 如果存在多个 form,那么提交信息的时候提交哪些信息,提交给哪个文件处理,这都 与提交按钮的位置有关.如果提交按钮在第一 ...
- Hadoop入门 集群崩溃的处理方法
目录 集群崩溃的处理方法 搞崩集群 错误示范 正确处理方法 1 回到hadoop的家目录 2 杀死进程 3 删除每个集群的data和logs 4 格式化 5 启动集群 总结 原因分析 集群崩溃的处理方 ...
- 日常Java 2021/10/13
Java枚举 values(), ordinal()和valueOf()方法位于java.lang.Enum类中: values()返回枚举类中所有的值 ordinal()方法可以找到每个枚举常量的索 ...
- 学习java 7.16
学习内容: 线程安全的类 Lock锁 生产者消费者模式 Object类的等待唤醒方法 明天内容: 网络编程 通信程序 遇到问题: 无
- A Child's History of England.23
King William, fearing he might lose his conquest, came back, and tried to pacify the London people b ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- STM32 CAN用队列缓冲接收的例子
[1]CAN接收用队列缓冲的例子: 发单帧没有问题,多帧或者连续发两帧就有问题.
- Linux磁盘分区(二)之挂载卸载常用命令
Linux磁盘分区(二)之挂载卸载常用命令 转自:https://blog.csdn.net/qq_36183935/article/details/81053383 https: ...