329. Longest Increasing Path in a Matrix
Last updated
Was this helpful?
Last updated
Was this helpful?
Given an m x n
integers matrix
, return the length of the longest increasing path inmatrix
.
From each cell, you can either move in four directions: left, right, up, or down. You may not move diagonally or move outside the boundary (i.e., wrap-around is not allowed).
Example 1:
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
0 <= matrix[i][j] <= 2^31 - 1
Depth-first Search, Topological Sort, Memoization
We build a 2D array dp
with the same size of matrix
to store the Longest Increasing Path starting from matrix[i][j]
, with all elements initialized with 1. We also need an 1D array of all elements in descending order. It allows us to find the longest path in decreasing way. Then we perform BFS for each elements of this 1D array, and update the longest path.
Time complexity:
Space complexity: