Given a right triangle of numbers, find the largest of the sum of numbers that appear on the paths starting from the top towards the base, so that on each path the next number is located directly below or below-and-one-place-to-the-right.
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
The idea is to find largest sum ending at every cell of last row and return maximum of these sums. We can recursively compute these sums by recursively considering above two cells. Since there are overlapping subproblems, we use dynamic programming to find the maximum sum ending at particular cell of last row.
Below is the implementation of above idea.
This article is contributed by Harshit Agrawal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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 path sum in an Inverted triangle | SET 2
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Maximum number of squares that can fit in a right angle isosceles triangle
- Minimum Sum Path in a Triangle
- Minimum length of the shortest path of a triangle
- Maximum Perimeter Triangle from array
- Maximum area of triangle having different vertex colors
- Maximum height when coins are arranged in a triangle
- Maximum sum path in a Matrix
- Path with maximum average value
- Maximum sum path in a matrix from top to bottom
- Maximum path sum in the given arrays with at most K jumps
- Maximum sum path in a matrix from top to bottom and back
- Maximum decimal value path in a binary matrix
- Maximum path sum for each position with jumps under divisibility condition
- Paths from entry to exit in matrix and maximum path sum
- Maximum weight path ending at any element of last row in a matrix
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
Improved By : jit_t