Problem Statement : Starting from top to bottom and moving to adjacent numbers below, find the maximum total for triangle.

By looking it at the given triangle for few minutes you would see that below is the path having maximum sum i.e. 23.

First approach that may pop in one's mind to solve this problem would be a brute force algorithm. This would be trying every route to find the max sum. The actual number of iterations that will take place for this approach would be 2n, which would be a problem if size of triangle increases.

By looking it at the given triangle for few minutes you would see that below is the path having maximum sum i.e. 23.

First approach that may pop in one's mind to solve this problem would be a brute force algorithm. This would be trying every route to find the max sum. The actual number of iterations that will take place for this approach would be 2n, which would be a problem if size of triangle increases.
Bottom to Top Approach
This approach is looking the triangle from bottom to top. First visualize the triangle as follows:

We transform the triangle in sort of tree structure and then only 2 steps need to be repeated until we reach the root node.
Step 1: Choose the max leaf node from the two child leaf nodes.
Step 2: Add the chosen leaf node to the parent node.
Repeat this until you get to root node. For this triangle this is how it goes, chose the max leaf nodes among the pairs.

Add the chosen nodes to parent.

These steps are repeated until we get to root node.

which gives us 23 the answer.
Comments
Post a Comment