Created
January 5, 2019 02:16
-
-
Save ttsugriy/0e4c62cfe070ddc2d8dd3108078335c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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