20240809 Finished

This commit is contained in:
jackfiled 2024-08-09 11:07:47 +08:00
parent 9b6d2f7d39
commit 7fab520904
2 changed files with 48 additions and 1 deletions

View File

@ -198,4 +198,5 @@ mod p70_climbing_stairs;
mod p198_house_robber;
mod p139_word_break;
mod p300_longest_increasing_subsequence;
mod p120_triangle;
mod p120_triangle;
mod p64_minimum_path_sum;

View File

@ -0,0 +1,46 @@
/**
* [64] Minimum Path Sum
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_path_sum(grid: Vec<Vec<i32>>) -> i32 {
let (m, n) = (grid.len(), grid[0].len());
let mut dp = vec![vec![0; n]; m];
for i in 0..m {
for j in 0..n {
if i == 0 {
if j == 0 {
dp[i][j] = grid[i][j];
} else {
dp[i][j] = dp[i][j - 1] + grid[i][j];
}
} else {
if j == 0 {
dp[i][j] = dp[i - 1][j] + grid[i][j];
} else {
dp[i][j] = dp[i - 1][j].min(dp[i][j - 1]) + grid[i][j];
}
}
}
}
dp[m - 1][n - 1]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_64() {
assert_eq!(7, Solution::min_path_sum(vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]]));
}
}