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.
- 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
- Path with maximum average value
- Maximum sum path in a matrix from top to bottom
- Maximum Perimeter Triangle from array
- Maximum decimal value path in a binary matrix
- Maximum height when coins are arranged in a triangle
- Maximum area of triangle having different vertex colors
- Maximum path sum for each position with jumps under divisibility condition
- Maximum weight path ending at any element of last row in a matrix
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
Improved By : jit_t