1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public int maxValue(int[][] grid) { int n1 = grid.length; int n2 = grid[0].length; int[][] dp = new int[n1][n2]; dp[0][0] = grid[0][0]; for (int i = 1; i < n1; i++) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } for (int i = 1; i < n2; i++) { dp[0][i] = dp[0][i - 1] + grid[0][i]; }
for (int i = 1; i < n1; i++) { for (int j = 1; j < n2; j++) { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } }
return dp[n1 - 1][n2 - 1]; } }
|