Given numbers in form of an Inverted triangle. By starting at the bottom of the triangle and moving to adjacent numbers on the row above, find the maximum total from bottom to top.
Input : 1 5 3 4 8 1 Output : 14 Input : 8 5 9 3 2 4 6 7 4 3 Output : 23
Approach : In the previous article we saw an approach of the problem where the triangle is non-inverted.
Here also we will use the same approach to find the solution of the problem as discussed in previous article.
If we should left shift every element and put 0 at each empty position to make it a regular matrix, then our problem looks like minimum cost path.
So, after converting our input triangle elements into a regular matrix we should apply the dynamic programming concept to find the maximum path sum.
Applying, DP in bottom-up manner we should solve our problem as:
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
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum path sum in a triangle.
- Maximum sum of a path in a Right Number Triangle
- Minimum Sum Path in a Triangle
- Minimum length of the shortest path of a triangle
- Maximum Perimeter Triangle from array
- Maximum of all the integers in the given level of Pascal triangle
- Minimum and maximum possible length of the third side of a triangle
- Maximum height when coins are arranged in a triangle
- Maximum area of triangle having different vertex colors
- Path with maximum average value
- Maximum sum path in a Matrix
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Maximum area of rectangle inscribed in an equilateral triangle
- Maximum number of squares that can fit in a right angle isosceles triangle
- Maximum sum path in a matrix from top to bottom
- Maximum path sum in the given arrays with at most K jumps
- Maximum decimal value path in a binary matrix
- Maximum sum path in a matrix from top to bottom and back
- Maximum weight path ending at any element of last row in a matrix
- Paths from entry to exit in matrix and maximum path sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.