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

📅  最后修改于: 2021-05-07 08:39:43             🧑  作者: 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
?>


输出:
14