0%

『LeetCode』剑指 Offer 47 礼物的最大价值

题目

剑指 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

标签

数组, 动态规划, 矩阵


题解

【礼物的最大价值】动态规划&滚动数组优化

解析

很基础的动态规划了,相当于从左上角走到右下角,所经过的格子的数值总和。

又每次行动只能向下或向右移动一格,换句话说,一个格子只能由它的上方或左方走到,所以很容易得到状态转移方程:

\[ dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j] \]

其中 \(dp[i][j]\) 表示走到坐标为 \((i,j)\) 的格子所能拿到礼物的最大值,转移公式的含义是从上方 (\(dp[i - 1][j]\)) 和左方 (\(dp[i][j - 1]\)) 选择能拿礼物多的那个路径,加上当前位置能拿礼物的价值,就是当前格子所能拿到礼物的最大值

动态规划

对于边界情况,我们有两种常用处理办法

  • 单独初始化边界
  • 多开一行一列数组,就能统一化边界处理

方法一、单独处理边界

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Code language: Python
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:
n, m = len(grid), len(grid[0])
dp = [[0 for _ in range(m)] for _ in range(n)]

dp[0][0] = grid[0][0]
# 初始化第一行
for i in range(1, m):
dp[0][i] = dp[0][i - 1] + grid[0][i]
# 初始化第一列
for i in range(1, n):
dp[i][0] = dp[i - 1][0] + grid[i][0]

for i in range(1, n):
for j in range(1, m):
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]

return dp[n - 1][m - 1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Code language: Java
class Solution {
private static int[][] dp = new int[207][207];
public int maxValue(int[][] grid) {
int n = grid.length, m = grid[0].length;
dp[0][0] = grid[0][0];
// 初始化第一行
for (int i = 1; i < m; ++i)
dp[0][i] = dp[0][i - 1] + grid[0][i];
// 初始化第一列
for (int i = 1; i < n; ++i)
dp[i][0] = dp[i - 1][0] + grid[i][0];
// 动态规划
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];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Code language: C++
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
vector<vector<int>> dp(n, vector<int>(m, 0));
dp[0][0] = grid[0][0];
// 初始化第一行
for (int i = 1; i < m; ++i)
dp[0][i] = dp[0][i - 1] + grid[0][i];
// 初始化第一列
for (int i = 1; i < n; ++i)
dp[i][0] = dp[i - 1][0] + grid[i][0];
// 动态规划
for (int i = 1; i < n; ++i){
for (int 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];
}
};

方法二、多开一行一列数组,就能统一化边界处理,不过需要做坐标偏移,即 \(dp[i][j]\) 表示走到坐标为 \((i - 1,j - 1)\) 的格子所能拿到礼物的最大值,相比较上一种方法优点是编程实现更简单

1
2
3
4
5
6
7
8
9
10
11
# Code language: Python
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:
n, m = len(grid), len(grid[0])
dp = [[0 for _ in range(m + 1)] for _ in range(n + 1)]

for i in range(1, n + 1):
for j in range(1, m + 1):
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1]

return dp[n][m]
1
2
3
4
5
6
7
8
9
10
11
12
13
// Code language: Java
class Solution {
private static int[][] dp = new int[207][207];
public int maxValue(int[][] grid) {
int n = grid.length, m = grid[0].length;
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 - 1][j - 1];
}
}
return dp[n][m];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Code language: C++
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
}
}
return dp[n][m];
}
};

  • 时间复杂度: \(O(n \times m)\)
  • 空间复杂度: \(O(n \times m)\)

滚动数组优化

考虑到 \(dp[i][j]\) 的值只与 \(dp[i-1][j]\)\(dp[i][j-1]\) 有关,我们可以用一个一维数组来代替二维的数组

1
2
3
4
5
6
7
8
9
10
11
# Code language: Python
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:
n, m = len(grid), len(grid[0])
dp = [0 for _ in range(m + 1)]

for i in range(1, n + 1):
for j in range(1, m + 1):
dp[j] = max(dp[j], dp[j - 1]) + grid[i - 1][j - 1]

return dp[m]
1
2
3
4
5
6
7
8
9
10
11
12
13
// Code language: Java
class Solution {
public int maxValue(int[][] grid) {
int n = grid.length, m = grid[0].length;
int[] dp = new int[m + 1];
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
dp[j] = Math.max(dp[j], dp[j - 1]) + grid[i - 1][j - 1];
}
}
return dp[m];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Code language: C++
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
vector<int> dp(m + 1, 0);
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
dp[j] = max(dp[j], dp[j - 1]) + grid[i - 1][j - 1];
}
}
return dp[m];
}
};

  • 时间复杂度: \(O(n \times m)\)
  • 空间复杂度: \(O(m)\)

记忆化dfs

这题也能用记忆化dfs求解,这里只展示python代码(cache装饰器对于记忆化dfs太友好惹~)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Code language: Python
from functools import cache
class Solution:
def maxValue(self, grid: List[List[int]]) -> int:

@cache
def dfs(i: int, j: int):
if i == 0 and j == 0:
return grid[i][j]
elif i == 0:
return dfs(i, j - 1) + grid[i][j]
elif j == 0:
return dfs(i - 1, j) + grid[i][j]
else:
return max(dfs(i - 1, j), dfs(i, j - 1)) + grid[i][j]

return dfs(len(grid) - 1, len(grid[0]) - 1)
  • 时间复杂度: \(O(n \times m)\)
  • 空间复杂度: \(O(n \times m)\)

如果对你有帮助的话,请给我点个赞吧~

欢迎前往 我的博客Algorithm - Github 查看更多题解

--- ♥ end ♥ ---

欢迎关注我呀~