20240809 Finished
This commit is contained in:
parent
9b6d2f7d39
commit
7fab520904
|
@ -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;
|
46
src/problem/p64_minimum_path_sum.rs
Normal file
46
src/problem/p64_minimum_path_sum.rs
Normal 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]]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user