Skip to content

Instantly share code, notes, and snippets.

@ttsugriy
Created January 5, 2019 02:16
Show Gist options
  • Save ttsugriy/0e4c62cfe070ddc2d8dd3108078335c1 to your computer and use it in GitHub Desktop.
Save ttsugriy/0e4c62cfe070ddc2d8dd3108078335c1 to your computer and use it in GitHub Desktop.
impl Solution {
pub fn min_falling_path_sum(a: Vec<Vec<i32>>) -> i32 {
let mut prev: Vec<i32> = a[0].clone();
let mut curr: Vec<i32> = vec![0; a[0].len()];
for i in 1..a.len() {
for j in 0..a[i].len() {
let mut sum_so_far = prev[j];
if j > 0 {
sum_so_far = std::cmp::min(sum_so_far, prev[j-1]);
}
if j < a[i].len() - 1 {
sum_so_far = std::cmp::min(sum_so_far, prev[j+1]);
}
curr[j] = a[i][j] + sum_so_far;
}
std::mem::swap(&mut curr, &mut prev);
}
*prev.iter().min().unwrap()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment