本问题对应的 leetcode 原文链接:剑指 Offer 47. 礼物的最大价值

问题描述

在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?

示例 1:

输入: 
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
输出: 12
解释: 路径 1→3→5→2→1 可以拿到最多价值的礼物

提示:

  • 0 < grid.length <= 200
  • 0 < grid[0].length <= 200

解题思路

视频讲解直达: 本题视频讲解

代码实现

class Solution {
    public int maxValue(int[][] grid) {
        //优化版本
        int n = grid.length;
        int m = grid[0].length;

        int[]dp = new int[m];
        dp[0] = grid[0][0];

        for(int j = 1; j < m; j++){
            dp[j] = dp[j-1] + grid[0][j];
        }

        for(int i = 1; i < n; i++){
            //
            dp[0] =  dp[0] + grid[i][0];
            for(int j = 1; j < m; j++){
                dp[j] = Math.max(dp[j], dp[j-1]) + grid[i][j];
            }
        }
        return dp[m-1];
    }
  // 二维版本
      public int maxValue(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;

        int[][] dp = new int[n][m];
        dp[0][0] = grid[0][0];
        for(int i = 1; i < n; i++){
            dp[i][0] = dp[i-1][0] + grid[i][0];
        }
        for(int j = 1; j < m; j++){
            dp[0][j] = dp[0][j -1] + grid[0][j];
        }

        for(int i = 1; i < n; i++){
            for(int j = 1; j < m; j++){
                dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]) + grid[i][j];
            }
        }

        return dp[n-1][m-1];
    }
}

时间复杂度:O(n*m)
空间复杂度:O(n)

Python

class Solution(object):
    def maxValue(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        n = len(grid)
        m = len(grid[0])
        dp = [0] * m
        dp[0] = grid[0][0]
        for j in range(1, m):
            dp[j] = dp[j-1] + grid[0][j]
        for i in range(1, n):
            dp[0] = dp[0] + grid[i][0]
            for j in range(1, m):
                dp[j] = max(dp[j], dp[j-1]) + grid[i][j]
        return dp[m-1]

C++

class Solution {
public:
    int maxValue(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        vector<int> dp(m, 0);
        dp[0] = grid[0][0];
        for (int j = 1; j < m; j++) {
            dp[j] = dp[j-1] + grid[0][j];
        }
        for (int i = 1; i < n; i++) {
            dp[0] += grid[i][0];
            for (int j = 1; j < m; j++) {
                dp[j] = max(dp[j-1], dp[j]) + grid[i][j];
            }
        }
        return dp[m-1];
    }
};

Go

func maxValue(grid [][]int) int {
    n, m := len(grid), len(grid[0])
    dp := make([]int, m)
    dp[0] = grid[0][0]
    for j := 1; j < m; j++ {
        dp[j] = dp[j-1] + grid[0][j]
    }
    for i := 1; i < n; i++ {
        dp[0] += grid[i][0]
        for j := 1; j < m; j++ {
            dp[j] = max(dp[j-1], dp[j]) + grid[i][j]
        }
    }
    return dp[m-1]
}

func maxValue2(grid [][]int) int {
    n, m := len(grid), len(grid[0])
    dp := make([][]int, n)
    for i := range dp {
        dp[i] = make([]int, m)
    }
    dp[0][0] = grid[0][0]
    for i := 1; i < n; i++ {
        dp[i][0] = dp[i-1][0] + grid[i][0]
    }
    for j := 1; j < m; j++ {
        dp[0][j] = dp[0][j-1] + grid[0][j]
    }
    for i := 1; i < n; i++ {
        for j := 1; j < m; j++ {
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + grid[i][j]
        }
    }
    return dp[n-1][m-1]
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}

JS

/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxValue = function(grid) {
    // 优化版本
    let n = grid.length;
    let m = grid[0].length;

    let dp = new Array(m).fill(0);
    dp[0] = grid[0][0];

    for (let j = 1; j < m; j++) {
        dp[j] = dp[j - 1] + grid[0][j];
    }

    for (let i = 1; i < n; i++) {
        dp[0] = dp[0] + grid[i][0];
        for (let j = 1; j < m; j++) {
            dp[j] = Math.max(dp[j], dp[j - 1]) + grid[i][j];
        }
    }
    return dp[m - 1];

    // 二维版本
    /*
    let n = grid.length;
    let m = grid[0].length;

    let dp = new Array(n);
    for (let i = 0; i < n; i++) {
        dp[i] = new Array(m).fill(0);
    }

    dp[0][0] = grid[0][0];
    for (let i = 1; i < n; i++) {
        dp[i][0] = dp[i - 1][0] + grid[i][0];
    }
    for (let j = 1; j < m; j++) {
        dp[0][j] = dp[0][j - 1] + grid[0][j];
    }

    for (let i = 1; i < n; i++) {
        for (let j = 1; j < m; j++) {
            dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
        }
    }

    return dp[n - 1][m - 1];
    */
};

发表回复

后才能评论