Smallest sum in a triangle, c++.
Go to solution
Solved by xypher,
Given the following triangle
1 6 89 4 1The solution is
1 8 1 = 10Two ways you can approach this problem are
- Bottom up
- Top down
I'll describe one possible way to get the answer using a bottom-up approach. Basically, you'll want to add the result from the previous row to the next row using the minimum. Then you're left with the answer.
Here's an example of the bottom up approach
// Starting with this 1 6 89 4 1// You'll start by taking the min of the first pair (9 and 4) and adding it to the item above (6)// Then you repeat for every pair in that row// Once the first set of calculations are done you'll be left with this 1 10 9// Because// min(9, 4) = 4 and 4 + 6 = 10// min(4, 1) = 1 and 1 + 8 = 9// Then you repeat for every row.// In this example after one more row you are left with your answer 10// Because// min(10, 9) = 9 and 9 + 1 = 10There are other ways to solve this problem, but this is the way I first figured it out on my own many years ago.
Hopefully this will help you get started. How you decide to implement it in code will be up to you to attempt. I want to help but I don't want to write it for you.
Thanks for the idea!

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now