Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2701 from kousukekikuchi1984/feature/l…
Browse files Browse the repository at this point in the history
…ongest-increasing-path-in-a-matrix

Create 0329-longest-increasing-path-in-a-matrix.rs
  • Loading branch information
tahsintunan authored Jul 22, 2023
2 parents 331d2a5 + 3b96efd commit df6e9b0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rust/0329-longest-increasing-path-in-a-matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
impl Solution {
pub fn longest_increasing_path(matrix: Vec<Vec<i32>>) -> i32 {
use std::cmp::max;
use std::collections::HashMap;

let mut dp: HashMap::<(usize, usize), i32> = HashMap::new();

fn dfs(
r: usize,
c: usize,
prevVal: i32,
matrix: &[Vec<i32>],
dp: &mut HashMap<(usize, usize), i32>,
) -> i32 {
if r < 0
|| r >= matrix.len()
|| c < 0
|| c >= matrix[0].len()
|| matrix[r][c] <= prevVal
{
return 0;
}
if let Some(&result) = dp.get(&(r, c)) {
return result;
}

let mut res = 1;
res = max(res, 1 + dfs(r + 1, c, matrix[r][c], matrix, dp));
res = max(res, 1 + dfs(r - 1, c, matrix[r][c], matrix, dp));
res = max(res, 1 + dfs(r, c + 1, matrix[r][c], matrix, dp));
res = max(res, 1 + dfs(r, c - 1, matrix[r][c], matrix, dp));
dp.insert((r, c), res);
res
}

for r in 0..matrix.len() {
for c in 0..matrix[0].len() {
dfs(r, c, -1, &matrix, &mut dp);
}
}
*dp.values().max().unwrap_or(&1)
}
}

0 comments on commit df6e9b0

Please sign in to comment.