#329

Longest Increasing Path in a Matrix

expert · 1255 · lc hard +32 · verified · 56.4% accepted · 9,496 likes · top 51%

play →

Description

Given an m x n integer matrix, find the longest strictly increasing path. From any cell you may move in four directions: left, right, up, or down. Diagonal moves and wrap-around are not allowed.

Return the length of the longest increasing path in matrix.

Example 1:

Input: matrix = [[9,9,4],[6,6,8],[2,1,1]]
Output: 4
Explanation: The longest increasing path is [1, 2, 6, 9].

Example 2:

Input: matrix = [[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.

Example 3:

Input: matrix = [[1]]
Output: 1

Code

1
2
3