给定一个由正整数组成的 NXN 矩阵 Mat[N][N]。一个单元格只有三种可能的移动 (i, j)
- (i+1, j)
- (i+1, j-1)
- (i+1, j+1)
从第 0 行的任何列开始,返回直到第 N-1 行的任何路径的最大总和。
例子:
Input : mat[4][4] = { {4, 2, 3, 4},
{2, 9, 1, 10},
{15, 1, 3, 0},
{16, 92, 41, 44} };
Output :120
path : 4 + 9 + 15 + 92 = 120
提问:亚马逊面试
上述问题可以递归定义。
假设初始位置为 MaximumPathSum(N-1, j),其中 j 从 0 到 N-1 变化。我们返回我们开始遍历的所有路径之间的最大值 (N-1, j) [其中 j 从 0 到 N-1]
i = N-1, j = 0 to N -1
int MaximumPath(Mat[][N], I, j)
// IF we reached to first row of
// matrix then return value of that
// element
IF ( i == 0 && j = 0 )
return Mat[i][j]
// out of matrix bound
IF( i = N || j < 0 )
return 0;
// call all rest position that we reached
// from current position and find maximum
// between them and add current value in
// that path
return max(MaximumPath(Mat, i-1, j),
MaximumPath(Mat, i-1, j-1),
MaximumPath(Mat, i-1, j+1)))
+ Mat[i][j];
如果我们绘制上述递归解的递归树,我们可以观察到重叠的子问题。由于问题有重叠的子问题,我们可以使用动态规划有效地解决它。以下是基于动态规划的解决方案。
C++
// C++ program to find Maximum path sum
// start any column in row '0' and ends
// up to any column in row 'n-1'
#include
using namespace std;
#define N 4
// function find maximum sum path
int MaximumPath(int Mat[][N])
{
int result = 0 ;
// create 2D matrix to store the sum
// of the path
int dp[N][N+2];
// initialize all dp matrix as '0'
memset(dp, 0, sizeof(dp));
// copy all element of first column into
// 'dp' first column
for (int i = 0 ; i < N ; i++)
dp[0][i+1] = Mat[0][i];
for (int i = 1 ; i < N ; i++)
for (int j = 1 ; j <= N ; j++)
dp[i][j] = max(dp[i-1][j-1],
max(dp[i-1][j],
dp[i-1][j+1])) +
Mat[i][j-1] ;
// Find maximum path sum that end ups
// at any column of last row 'N-1'
for (int i=0; i<=N; i++)
result = max(result, dp[N-1][i]);
// return maximum sum path
return result ;
}
// driver program to test above function
int main()
{
int Mat[4][4] = { { 4, 2 , 3 , 4 },
{ 2 , 9 , 1 , 10},
{ 15, 1 , 3 , 0 },
{ 16 ,92, 41, 44 }
};
cout << MaximumPath ( Mat ) <
Java
// Java program to find Maximum path sum
// start any column in row '0' and ends
// up to any column in row 'n-1'
import java.util.*;
class GFG {
static int N = 4;
// function find maximum sum path
static int MaximumPath(int Mat[][])
{
int result = 0;
// create 2D matrix to store the sum
// of the path
int dp[][] = new int[N][N + 2];
// initialize all dp matrix as '0'
for (int[] rows : dp)
Arrays.fill(rows, 0);
// copy all element of first column into
// 'dp' first column
for (int i = 0; i < N; i++)
dp[0][i + 1] = Mat[0][i];
for (int i = 1; i < N; i++)
for (int j = 1; j <= N; j++)
dp[i][j] = Math.max(dp[i - 1][j - 1],
Math.max(dp[i - 1][j],
dp[i - 1][j + 1])) +
Mat[i][j - 1];
// Find maximum path sum that end ups
// at any column of last row 'N-1'
for (int i = 0; i <= N; i++)
result = Math.max(result, dp[N - 1][i]);
// return maximum sum path
return result;
}
// driver code
public static void main(String arg[])
{
int Mat[][] = { { 4, 2, 3, 4 },
{ 2, 9, 1, 10 },
{ 15, 1, 3, 0 },
{ 16, 92, 41, 44 } };
System.out.println(MaximumPath(Mat));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to find
# Maximum path sum
# start any column in
# row '0' and ends
# up to any column in row 'n-1'
N = 4
# function find maximum sum path
def MaximumPath(Mat):
result = 0
# create 2D matrix to store the sum
# of the path
# initialize all dp matrix as '0'
dp = [[0 for i in range(N+2)] for j in range(N)]
# copy all element of first column into
# dp first column
for i in range(N):
for j in range(1, N+1):
dp[i][j] = max(dp[i-1][j-1],
max(dp[i-1][j],
dp[i-1][j+1])) + \
Mat[i][j-1]
# Find maximum path sum that end ups
# at any column of last row 'N-1'
for i in range(N+1):
result = max(result, dp[N-1][i])
# return maximum sum path
return result
# driver program to test above function
Mat = [[4, 2, 3, 4],
[2, 9, 1, 10],
[15, 1, 3, 0],
[16, 92, 41, 44]]
print(MaximumPath(Mat))
# This code is contributed by Soumen Ghosh.
C#
// C# program to find Maximum path sum
// start any column in row '0' and ends
// up to any column in row 'n-1'
using System;
class GFG {
static int N = 4;
// function find maximum sum path
static int MaximumPath(int [,] Mat)
{
int result = 0;
// create 2D matrix to store the sum
// of the path
int [,]dp = new int[N,N + 2];
// initialize all dp matrix as '0'
//for (int[] rows : dp)
// Arrays.fill(rows, 0);
// copy all element of first column into
// 'dp' first column
for (int i = 0; i < N; i++)
dp[0,i + 1] = Mat[0,i];
for (int i = 1; i < N; i++)
for (int j = 1; j <= N; j++)
dp[i,j] = Math.Max(dp[i - 1,j - 1],
Math.Max(dp[i - 1,j],
dp[i - 1,j + 1])) +
Mat[i,j - 1];
// Find maximum path sum that end ups
// at any column of last row 'N-1'
for (int i = 0; i <= N; i++)
result = Math.Max(result, dp[N - 1,i]);
// return maximum sum path
return result;
}
// driver code
public static void Main()
{
int [,]Mat = { { 4, 2, 3, 4 },
{ 2, 9, 1, 10 },
{ 15, 1, 3, 0 },
{ 16, 92, 41, 44 } };
Console.WriteLine(MaximumPath(Mat));
}
}
// This code is contributed by Ryuga.
PHP
Javascript
输出:
120
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。