基础版
class Solution {
public int maxValue(int[][] grid) {
if(grid == null || grid.length == 0) return -1;
int row = grid.length;
int col = grid[0].length;
int[][] res = new int[row+1][col+1];
for(int i=1; i<=row; ++i){
for(int j=1; j<=col; ++j){
res[i][j] = Math.max(res[i-1][j],res[i][j-1])+grid[i-1][j-1];
}
}
return res[row][col];
}
}
升级版 压缩dp
class Solution {
public int maxValue(int[][] grid) {
if(grid == null || grid.length == 0) return -1;
int row = grid.length;
int col = grid[0].length;
int[] res = new int[col+1];
for(int i=1; i<=row; ++i){
for(int j=1; j<=col; ++j){
res[j] = Math.max(res[j],res[j-1])+grid[i-1][j-1];
}
}
return res[col];
}
}
欢迎大家评论,如果本文对您有帮助,请点个赞,您的点赞对我很重要!这次一定!感谢!!!
转发请注明出处呦!感谢!!!