diff --git a/rs/minimum-falling-path-sum.rs b/rs/minimum-falling-path-sum.rs new file mode 100644 index 0000000..f216456 --- /dev/null +++ b/rs/minimum-falling-path-sum.rs @@ -0,0 +1,29 @@ +impl Solution { + fn get(matrix: &[Vec], row: isize, col: isize) -> Option { + let rows = matrix.len() as isize; + if row < 0 || row >= rows { + return None; + } + + let cols = matrix[row as usize].len() as isize; + if col < 0 || col >= cols { + return None; + } + + Some(matrix[row as usize][col as usize]) + } + + pub fn min_falling_path_sum(mut matrix: Vec>) -> i32 { + for row in (0..matrix.len() - 1).rev() { + for col in 0..matrix[row].len() { + matrix[row][col] += vec![-1, 0, 1] + .into_iter() + .filter_map(|dcol| Self::get(&matrix, row as isize + 1, col as isize + dcol)) + .min() + .unwrap() + } + } + + *matrix.first().unwrap().iter().min().unwrap() + } +}