剑指 Offer 47. 礼物的最大价值

class Solution {
    public int maxValue(int[][] grid) {
        int rows = grid.length;
        int cols = grid[0].length;

        //int dp[0][0] = grid[0][0];
        for(int i=0;i<rows ;i++){
            for(int j=0;j<cols;j++){
                if(i==0 && j==0){
                    grid[i][j] = grid[i][j];
                }else if(i==0 && j!= 0){
                    grid[i][j] = grid[i][j] + grid[i][j-1];
                }else if(i!=0 && j==0){
                    grid[i][j] = grid[i][j] + grid[i-1][j];
                }else{
                    grid[i][j] = grid[i][j] + Math.max(grid[i-1][j],grid[i][j-1]);
                }

            }
        }
        return grid[rows-1][cols-1];
    }
}

 

posted @ 2020-12-16 19:07  peanut_zh  阅读(77)  评论(0编辑  收藏  举报