leetcode Longest Increasing Path in a Matrix

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

 

题目地址:leetcode Longest Increasing Path in a Matrix

题意:给定一个矩阵,在里面找出最大上升路径

思路:

记忆化搜索。

设dis[i][j]为当前点出发最大上升路径的值。初始设置为0,表示该点未知,需要更新。

再次碰到的时候只需要返回该值即可。

C++

Java

 

 

Python

 

本文是leetcode 329 Longest Increasing Path in a Matrix  的题解,更多题解可见

https://www.hrwhisper.me/leetcode-algorithm-solution/

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Longest Increasing Path in a Matrix
本文地址:https://www.hrwhisper.me/leetcode-longest-increasing-path-matrix/

打赏一杯咖啡钱呗

Leetcode , , , . permalink.

4 thoughts on “leetcode Longest Increasing Path in a Matrix

Leave a Reply

Your email address will not be published. Required fields are marked *