1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pub struct Solution;
#[allow(unused)]
impl Solution {
pub fn max_value(grid: Vec<Vec<i32>>) -> i32 {
if grid.is_empty() {
return 0
}
let mut dp = vec![vec![0; grid[0].len()]; grid.len()];
for i in 0..grid.len() {
for j in 0..grid[0].len() {
if i == 0 && j == 0 {
dp[i][j] = grid[i][j];
} else if i == 0 {
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] = std::cmp::max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
}
dp[grid.len() - 1][grid[0].len() - 1]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let grid = vec![vec![1, 3, 1], vec![1, 5, 1], vec![4, 2, 1]];
assert_eq!(Solution::max_value(grid), 12);
}
}