给定尺寸为N * M的矩阵mat [] [] ,任务是查找从给定的左上单元(0,0)到右下单元(N – 1,M – 1)的路径矩阵,使得路径中元素的总和最大。从矩阵的任何单元格(i,j)允许的唯一移动是(i + 1,j)或(i,j + 1) 。
例子:
Input: mat[][] = {{3, 7}, {9, 8}}
Output: 20
Explanation:
Path with maximum sum is 3 => 9 => 8 as 20.
Input: mat[][] = {{1, 2}, {3, 5}}
Output: 9
Explanation:
Path with maximum sum is 1 => 3 => 5 as 9
方法:想法是使用动态编程来解决此问题。关键的观察结果是只能从mat [i – 1] [j]或mat [i] [j – 1]到达单元格mat [i] [j ] 。因此,该问题的递归关系由下式给出:
sum(i, j) = max(sum(i – 1, j), sum(i, j – 1)) + mat[i][j]
- 初始化尺寸为N * M的辅助矩阵sum [] [] 。
- 使用上面形成的递归关系,对矩阵元素进行迭代并更新辅助矩阵sum [] []的每个单元。
- 完成上述步骤后,将值总和[N] [M]将包含从左上角到给定矩阵的右下角的路径的最大总和可能的。打印该笔款项。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum sum
// path in the grid
int MaximumPath(vector >& grid)
{
// Dimensions of grid[][]
int N = grid.size();
int M = grid[0].size();
// Stores maximum sum at each cell
// sum[i][j] from cell sum[0][0]
vector > sum;
sum.resize(N + 1,
vector(M + 1));
// Iterate to compute the maximum
// sum path in the grid
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
// Update the maximum path sum
sum[i][j] = max(sum[i - 1][j],
sum[i][j - 1])
+ grid[i - 1][j - 1];
}
}
// Return the maximum sum
return sum[N][M];
}
// Driver Code
int main()
{
vector > grid
= { { 1, 2 }, { 3, 5 } };
cout << MaximumPath(grid);
return 0;
}
Java
// Java program for
//the above approach
import java.util.*;
class GFG{
// Function to find the maximum sum
// path in the grid
static int MaximumPath(int [][]grid)
{
// Dimensions of grid[][]
int N = grid.length;
int M = grid[0].length;
// Stores maximum sum at each cell
// sum[i][j] from cell sum[0][0]
int [][]sum = new int[N + 1][M + 1];
// Iterate to compute the maximum
// sum path in the grid
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= M; j++)
{
// Update the maximum path sum
sum[i][j] = Math.max(sum[i - 1][j],
sum[i][j - 1]) +
grid[i - 1][j - 1];
}
}
// Return the maximum sum
return sum[N][M];
}
// Driver Code
public static void main(String[] args)
{
int [][]grid = {{1, 2}, {3, 5}};
System.out.print(MaximumPath(grid));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to find the maximum sum
# path in the grid
def MaximumPath(grid):
# Dimensions of grid[][]
N = len(grid)
M = len(grid[0])
# Stores maximum sum at each cell
# sum[i][j] from cell sum[0][0]
sum = [[0 for i in range(M + 1)]
for i in range(N + 1)]
# Iterate to compute the maximum
# sum path in the grid
for i in range(1, N + 1):
for j in range(1, M + 1):
# Update the maximum path sum
sum[i][j] = (max(sum[i - 1][j],
sum[i][j - 1]) +
grid[i - 1][j - 1])
# Return the maximum sum
return sum[N][M]
# Driver Code
if __name__ == '__main__':
grid = [ [ 1, 2 ], [ 3, 5 ] ]
print(MaximumPath(grid))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum sum
// path in the grid
static int MaximumPath(int [,]grid)
{
// Dimensions of grid[,]
int N = grid.GetLength(0);
int M = grid.GetLength(1);
// Stores maximum sum at each cell
// sum[i,j] from cell sum[0,0]
int [,]sum = new int[N + 1, M + 1];
// Iterate to compute the maximum
// sum path in the grid
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= M; j++)
{
// Update the maximum path sum
sum[i, j] = Math.Max(sum[i - 1, j],
sum[i, j - 1]) +
grid[i - 1, j - 1];
}
}
// Return the maximum sum
return sum[N, M];
}
// Driver Code
public static void Main(String[] args)
{
int [,]grid = { { 1, 2 }, { 3, 5 } };
Console.Write(MaximumPath(grid));
}
}
// This code is contributed by Amit Katiyar
输出:
9
时间复杂度: O(N * M)
辅助空间: O(N * M)