📜  倒三角形中的最大路径和 |设置 2

📅  最后修改于: 2021-09-22 09:58:39             🧑  作者: Mango

给定倒三角形形式的数字。通过从三角形的底部开始移动到上面一行的相邻数字,找到从下到上的最大总数。
例子:

Input : 1 5 3
         4 8
          1
Output : 14

Input : 8 5 9 3
         2 4 6
          7 4
           3
Output : 23

方法:在上一篇文章中,我们看到了三角形不倒置问题的方法。
在这里,我们也将使用与上一篇文章中讨论的相同的方法来找到问题的解决方案。
如果我们应该左移每个元素并在每个空位置放 0 以使其成为常规矩阵,那么我们的问题看起来像最小成本路径。
因此,在将我们的输入三角形元素转换为正则矩阵后,我们应该应用动态规划概念来找到最大路径和。
以自下而上的方式应用 DP 我们应该将我们的问题解决为:
例子 :

8 5 9 3
 2 4 6
  7 4
   3

Step 1 :
8 5 9 3
2 4 6 0
7 4 0 0
3 0 0 0

Step 2 :
8 5 9 3
2 4 6 0
10 7 0 0

Step 3 :
8 5 9 3
12 14 13 0

Step 4:
20 19 23 16

Output : 23

下面是上述方法的实现:

C++
// C++ program implementation of
// Max sum problem in a triangle
#include 
using namespace std;
#define N 3
 
// Function for finding maximum sum
int maxPathSum(int tri[][N])
{
    int ans = 0;
 
    // Loop for bottom-up calculation
    for (int i = N - 2; i >= 0; i--) {
        for (int j = 0; j < N - i; j++) {
 
            // For each element, check both
            // elements just below the number
            // and below left to the number
            // add the maximum of them to it
            if (j - 1 >= 0)
                tri[i][j] += max(tri[i + 1][j],
                                 tri[i + 1][j - 1]);
            else
                tri[i][j] += tri[i + 1][j];
 
            ans = max(ans, tri[i][j]);
        }
    }
 
    // Return the maximum sum
    return ans;
}
 
// Driver Code
int main()
{
    int tri[N][N] = { { 1, 5, 3 },
                      { 4, 8, 0 },
                      { 1, 0, 0 } };
 
    cout << maxPathSum(tri);
 
    return 0;
}


Java
// Java program implementation of
// Max sum problem in a triangle
 
class GFG
{
    static int N = 3;
 
    // Function for finding maximum sum
    static int maxPathSum(int tri[][])
    {
        int ans = 0;
     
        // Loop for bottom-up calculation
        for (int i = N - 2; i >= 0; i--)
        {
            for (int j = 0; j < N - i; j++)
            {
     
                // For each element, check both
                // elements just below the number
                // and below left to the number
                // add the maximum of them to it
                if (j - 1 >= 0)
                    tri[i][j] += Math.max(tri[i + 1][j],
                                    tri[i + 1][j - 1]);
                else
                    tri[i][j] += tri[i + 1][j];
     
                ans = Math.max(ans, tri[i][j]);
            }
        }
     
        // Return the maximum sum
        return ans;
    }
     
    // Driver Code
    public static void main(String []args)
    {
        int tri[][] = { { 1, 5, 3 },
                        { 4, 8, 0 },
                        { 1, 0, 0 } };
     
        System.out.println(maxPathSum(tri));
    }
}
 
// This code is contributed by ihritik


Python3
# Python program implementation of
# Max sum problem in a triangle
 
N = 3
 
# Function for finding maximum sum
def maxPathSum( tri ):
 
    ans = 0;
 
    # Loop for bottom-up calculation
    for i in range(N - 2, -1, -1):
        for j in range(0 , N - i):
 
            # For each element, check both
            # elements just below the number
            # and below left to the number
            # add the maximum of them to it
            if (j - 1 >= 0):
                tri[i][j] += max(tri[i + 1][j],
                                tri[i + 1][j - 1]);
            else:
                tri[i][j] += tri[i + 1][j];
 
            ans = max(ans, tri[i][j]);
     
    # Return the maximum sum
    return ans
     
# Driver Code
     
tri = [ [ 1, 5, 3 ],
        [ 4, 8, 0 ],
        [ 1, 0, 0 ] ]
 
print(maxPathSum(tri))
 
# This code is contributed by ihritik


C#
// C# program implementation of
// Max sum problem in a triangle
using System;
 
class GFG
{
    static int N = 3;
 
    // Function for finding maximum sum
    static int maxPathSum(int [,]tri)
    {
        int ans = 0;
     
        // Loop for bottom-up calculation
        for (int i = N - 2; i >= 0; i--)
        {
            for (int j = 0; j < N - i; j++)
            {
     
                // For each element, check both
                // elements just below the number
                // and below left to the number
                // add the maximum of them to it
                if (j - 1 >= 0)
                    tri[i, j] += Math.Max(tri[i + 1, j],
                                    tri[i + 1, j - 1]);
                else
                    tri[i, j] += tri[i + 1, j];
     
                ans = Math.Max(ans, tri[i, j]);
            }
        }
     
        // Return the maximum sum
        return ans;
    }
     
    // Driver Code
    public static void Main()
    {
        int[,] tri = { { 1, 5, 3 },
                        { 4, 8, 0 },
                        { 1, 0, 0 } };
     
        Console.WriteLine(maxPathSum(tri));
    }
}
 
// This code is contributed by ihritik


PHP
= 0; $i--)
    {
        for ($j = 0; $j < $N - $i; $j++)
        {
 
            // For each element, check both
            // elements just below the number
            // and below left to the number
            // add the maximum of them to it
            if ($j - 1 >= 0)
                $tri[$i][$j] += max($tri[$i + 1][$j],
                                    $tri[$i + 1][$j - 1]);
            else
                $tri[$i][$j] += $tri[$i + 1][$j];
 
            $ans = max($ans, $tri[$i][$j]);
        }
    }
 
    // Return the maximum sum
    return $ans;
}
 
// Driver Code
$tri = array(array( 1, 5, 3 ),
             array( 4, 8, 0 ),
             array( 1, 0, 0 ));
 
echo maxPathSum($tri);
 
// This code is contributed by chandan_jnu
?>


Javascript


输出:
14

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程