algo/rust/40_dynamic_programming/triangle.rs
caitlin.gao e2e02d4544 feat(geektime_algo): add 40 dynamic programming
bag, knapsack, triangle
2019-08-27 18:58:41 +08:00

24 lines
561 B
Rust

# leetcode [minimum_total](https://leetcode.com/problems/triangle/)
pub fn minimum_total(mut triangle: Vec<Vec<i32>>) -> i32 {
if triangle.len() == 0 { return 0; }
for i in (0..triangle.len() - 1).rev() {
for j in 0..triangle[i].len() {
triangle[i][j] = triangle[i+1][j].min(triangle[i+1][j+1]) + triangle[i][j];
}
}
triangle[0][0]
}
fn main() {
let triangle = vec![
vec![2],
vec![3, 4],
vec![6, 5, 7],
vec![4, 1, 8, 3],
];
println!("{:?}", minimum_total(triangle));
}