📜  直角三角形中路径的最大和

📅  最后修改于: 2021-05-04 17:57:39             🧑  作者: Mango

给定数字的直角三角形,找到出现在从顶部到底部的路径上的最大数字总和,以便在每个路径上,下一个数字直接位于以下位置或下方和一个位置-正确的。

例子 :

Input : 1
        1 2
        4 1 2
        2 3 1 1        
Output : 9
Explanation : 1 + 1 + 4 + 3

Input : 2
        4 1
        1 2 7
Output : 10
Explanation : 2 + 1 + 7

这个想法是找到以最后一行的每个单元格结尾的最大和,然后返回这些和的最大值。我们可以通过递归考虑以上两个单元来递归地计算这些总和。由于存在重叠的子问题,我们使用动态编程来找到以最后一行的特定单元格结尾的最大和。

以下是上述想法的实现。

C++
// C++ program to print maximum sum
// in a right triangle of numbers
#include
using namespace std;
  
// function to find maximum sum path
int maxSum(int tri[][3], int n)
{
    // Adding the element of row 1 to both the
    // elements of row 2 to reduce a step from
    // the loop
    if (n > 1)
        tri[1][1] = tri[1][1] + tri[0][0];
        tri[1][0] = tri[1][0] + tri[0][0];
  
    // Traverse remaining rows
    for(int i = 2; i < n; i++) {
        tri[i][0] = tri[i][0] + tri[i-1][0];
        tri[i][i] = tri[i][i] + tri[i-1][i-1];
  
        //Loop to traverse columns
        for (int j = 1; j < i; j++){
  
            // Checking the two conditions, 
            // directly below and below right.
            // Considering the greater one
              
            // tri[i] would store the possible 
            // combinations of sum of the paths
            if (tri[i][j] + tri[i-1][j-1] >= 
                            tri[i][j] + tri[i-1][j])
                  
                tri[i][j] = tri[i][j] + tri[i-1][j-1];
            else
                tri[i][j] = tri[i][j]+tri[i-1][j];
        }
    }
      
    // array at n-1 index (tri[i]) stores 
    // all possible adding combination, finding 
    // the maximum one out of them
    int max=tri[n-1][0];
      
    for(int i=1;i


Java
// Java program to print maximum sum
// in a right triangle of numbers
class GFG
{
      
    // function to find maximum sum path
    static int maxSum(int tri[][], int n)
    {
          
        // Adding the element of row 1 to both the
        // elements of row 2 to reduce a step from
        // the loop
        if (n > 1)
            tri[1][1] = tri[1][1] + tri[0][0];
            tri[1][0] = tri[1][0] + tri[0][0];
      
        // Traverse remaining rows
        for(int i = 2; i < n; i++) {
            tri[i][0] = tri[i][0] + tri[i-1][0];
            tri[i][i] = tri[i][i] + tri[i-1][i-1];
      
            //Loop to traverse columns
            for (int j = 1; j < i; j++){
      
                // Checking the two conditions, 
                // directly below and below right.
                // Considering the greater one
                  
                // tri[i] would store the possible 
                // combinations of sum of the paths
                if (tri[i][j] + tri[i-1][j-1] >= 
                           tri[i][j] + tri[i-1][j])
                      
                    tri[i][j] = tri[i][j] 
                                  + tri[i-1][j-1];
                      
                else
                    tri[i][j] = tri[i][j]
                                    + tri[i-1][j];
            }
        }
          
        // array at n-1 index (tri[i]) stores 
        // all possible adding combination, 
        // finding the maximum one out of them
        int max = tri[n-1][0];
          
        for(int i = 1; i < n; i++)
        {
            if(max < tri[n-1][i])
                max = tri[n-1][i];
        }
          
        return max;
    } 
          
    // Driver code
    public static void main (String[] args)
    {
        int tri[][] = {{1}, {2,1}, {3,3,2}};
          
        System.out.println(maxSum(tri, 3));
    }
}
  
// This code is contributed by Anant Agarwal.


Python
# Python program to print maximum sum
# in a right triangle of numbers.
  
# tri[][] is a 2D array that stores the
# triangle, n is number of lines or rows.
def maxSum(tri, n):
  
    # Adding the element of row 1 to both the
    # elements of row 2 to reduce a step from
    # the loop
    if n > 1:
        tri[1][1] = tri[1][1]+tri[0][0]
        tri[1][0] = tri[1][0]+tri[0][0]
  
    # Traverse remaining rows
    for i in range(2, n):
        tri[i][0] = tri[i][0] + tri[i-1][0]
        tri[i][i] = tri[i][i] + tri[i-1][i-1]
  
        # Loop to traverse columns
        for j in range(1, i):
  
            # Checking the two conditions, directly below
            # and below right. Considering the greater one
  
            # tri[i] would store the possible combinations
            # of sum of the paths
            if tri[i][j]+tri[i-1][j-1] >= tri[i][j]+tri[i-1][j]:
                tri[i][j] = tri[i][j] + tri[i-1][j-1]
            else:
                tri[i][j] = tri[i][j]+tri[i-1][j]
  
    # array at n-1 index (tri[i]) stores all possible
    # adding combination, finding the maximum one
    # out of them
    print max(tri[n-1])
  
# driver program
tri = [[1], [2,1], [3,3,2]]
maxSum(tri, 3)


C#
// C# program to print 
// maximum sum in a right
// triangle of numbers
using System;
  
class GFG
{
      
    // function to find 
    // maximum sum path
    static int maxSum(int [,]tri, 
                      int n)
    {
          
        // Adding the element of row 1 
        // to both the elements of row 2 
        // to reduce a step from the loop
        if (n > 1)
            tri[1, 1] = tri[1, 1] + 
                        tri[0, 0];
            tri[1, 0] = tri[1, 0] + 
                        tri[0, 0];
      
        // Traverse remaining rows
        for(int i = 2; i < n; i++) 
        {
            tri[i, 0] = tri[i, 0] + 
                        tri[i - 1, 0];
            tri[i, i] = tri[i, i] + 
                        tri[i - 1, i - 1];
       
            //Loop to traverse columns
            for (int j = 1; j < i; j++)
            {
      
                // Checking the two conditions, 
                // directly below and below right.
                // Considering the greater one
                  
                // tri[i] would store the possible 
                // combinations of sum of the paths
                if (tri[i, j] + tri[i - 1, j - 1] >= 
                    tri[i, j] + tri[i - 1, j])
                  
                    tri[i, j] = tri[i, j] + 
                                tri[i - 1, j - 1];
                      
                else
                    tri[i, j] = tri[i, j] + 
                                tri[i - 1, j];
            }
        }
          
        // array at n-1 index (tri[i]) 
        // stores all possible adding 
        // combination, finding the 
        // maximum one out of them
        int max = tri[n - 1, 0];
          
        for(int i = 1; i < n; i++)
        {
            if(max < tri[n - 1, i])
                max = tri[n - 1, i];
        }
          
        return max;
    } 
          
// Driver Code
public static void Main ()
{
      
        int [,]tri = {{1,0,0}, 
                      {2,1,0},
                      {3,3,2}};
          
        Console.Write(maxSum(tri, 3));
}
}
  
// This code is contributed by ajit.


PHP
 1)
        $tri[1][1] = $tri[1][1] + $tri[0][0];
        $tri[1][0] = $tri[1][0] + $tri[0][0];
  
    // Traverse remaining rows
    for($i = 2; $i < $n; $i++)
    {
        $tri[$i][0] = $tri[$i][0] + 
                      $tri[$i - 1][0];
        $tri[$i][$i] = $tri[$i][$i] + 
                       $tri[$i - 1][$i - 1];
  
        //Loop to traverse columns
        for ($j = 1; $j < $i; $j++)
        {
  
            // Checking the two conditions, 
            // directly below and below right.
            // Considering the greater one
              
            // tri[i] would store the possible 
            // combinations of sum of the paths
            if ($tri[$i][$j] + $tri[$i - 1][$j - 1] >= 
                 $tri[$i][$j] + $tri[$i - 1][$j])
                  
                $tri[$i][$j] = $tri[$i][$j] + 
                               $tri[$i - 1][$j - 1];
            else
                $tri[$i][$j] = $tri[$i][$j] + 
                               $tri[$i - 1][$j];
        }
    }
      
    // array at n-1 index (tri[i]) 
    // stores all possible adding 
    // combination, finding the 
    // maximum one out of them
      
    $max = $tri[$n - 1][0];
      
    for($i = 1; $i < $n; $i++)
    {
        if($max < $tri[$n - 1][$i])
            $max = $tri[$n - 1][$i];
    }
      
    return $max;
}
  
// Driver Code
$tri = array(array(1),
             array(2,1), 
             array(3,3,2));
  
echo maxSum($tri, 3);
  
// This code is contributed by ajit
?>


输出 :

6