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 firstname.lastname@example.org. 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
- Maximum sum path in a matrix from top-left to bottom-right
- Minimum Sum Path in a Triangle
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Check if right triangle possible from given area and hypotenuse
- Find other two sides of a right angle triangle
- Check whether right angled triangle is valid or not for large sides
- Find the dimensions of Right angled triangle
- Find other two sides and angles of a right angle triangle
- Check if a right-angled triangle can be formed by the given coordinates
- Area of Incircle of a Right Angled Triangle
- Find the hypotenuse of a right angled triangle with given two sides
- Find the height of a right-angled triangle whose area is X times its base
- Check if a right-angled triangle can be formed by moving any one of the coordinates
- Count of Right-Angled Triangle formed from given N points whose base or perpendicular are parallel to X or Y axis
- Minimum length of the shortest path of a triangle
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
Improved By : jit_t