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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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 height when coins are arranged in a triangle
- Minimum and maximum possible length of the third side of a triangle
- Maximum area of triangle having different vertex colors
- Maximum of all the integers in the given level of Pascal triangle
- Path with maximum average value
- Maximum sum path in a Matrix
- Maximum number of squares that can fit in a right angle isosceles triangle
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Maximum area of rectangle inscribed in an equilateral 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 path sum for each position with jumps under divisibility condition
- Maximum weight path ending at any element of last row in a matrix
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.