329. Longest Increasing Path in a Matrix
Given an m x n
matrix
, return the length of the longest increasing path in matrix
.
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:

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
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
0 <= matrix[i][j] <= 231 - 1
Rust Solution
struct Solution;
impl Solution {
fn longest_increasing_path(matrix: Vec<Vec<i32>>) -> i32 {
let n = matrix.len();
if n == 0 {
return 0;
}
let m = matrix[0].len();
let mut memo = vec![vec![0; m]; n];
let mut res = 0;
for i in 0..n {
for j in 0..m {
if memo[i][j] == 0 {
memo[i][j] = Self::dfs(i, j, &mut memo, &matrix);
res = res.max(memo[i][j]);
}
}
}
res
}
fn dfs(i: usize, j: usize, memo: &mut Vec<Vec<i32>>, matrix: &[Vec<i32>]) -> i32 {
if memo[i][j] != 0 {
return memo[i][j];
}
let n = matrix.len();
let m = matrix[0].len();
let mut res = 1;
if i > 0 && matrix[i - 1][j] > matrix[i][j] {
res = res.max(Self::dfs(i - 1, j, memo, matrix) + 1);
}
if j > 0 && matrix[i][j - 1] > matrix[i][j] {
res = res.max(Self::dfs(i, j - 1, memo, matrix) + 1);
}
if i + 1 < n && matrix[i + 1][j] > matrix[i][j] {
res = res.max(Self::dfs(i + 1, j, memo, matrix) + 1);
}
if j + 1 < m && matrix[i][j + 1] > matrix[i][j] {
res = res.max(Self::dfs(i, j + 1, memo, matrix) + 1);
}
memo[i][j] = res;
res
}
}
#[test]
fn test() {
let matrix = vec_vec_i32![[9, 9, 4], [6, 6, 8], [2, 1, 1]];
let res = 4;
assert_eq!(Solution::longest_increasing_path(matrix), res);
}
Having problems with this solution? Click here to submit an issue on github.