Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:28:54+00:00 2026-06-04T07:28:54+00:00

So, basically I’m feeling incredibly dumb, because of this exercise , I’ve spent like

  • 0

So, basically I’m feeling incredibly dumb, because of this exercise, I’ve spent like 4 or 5 hours trying to code it, and so far I’ve not been successful.

I have come to the realization that this one is easier to solve with a tree traversal using the Longest Path approach, But I’m not sure (Could you please confirm this to me.), could be over-kill since it’s supposed to be one of the easy problems, So Could you please help me with some guidance or basic steps or algorithm approaches on how to solve this one? all kinds of help is certainly appreciated.

PS. I usually post some code about what I’ve done so far, but I believe that to this point everything has been so wrong that I prefer to start from scratch, at least in terms of ideas.

Thanks.

As per-request, here’s the Code I typed according to the accepted answer to solve the Exercise:

def get_max_sum(matrix)
  (1...matrix.length).each do |index|  
    flag = 0  
    matrix[index].each_with_index do |e, i|    
      add = (matrix[index-1][flag] > matrix[index-1][flag+1]) ? matrix[index-1][flag] : matrix[index-1][flag+1]
      e += add
      matrix[index][i] = e
      flag = flag + 1
    end    
  end
  matrix[-1][0]
end

Where the matrix param is an array of arrays, each one representing a row from the triangle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:28:56+00:00Added an answer on June 4, 2026 at 7:28 am

    This problem is easy if you start from the bottom and work your way up. Consider the triangle

       1
      1 2
     4 1 2
    2 3 1 1
    

    Look at the next-to-last row. If by some path through the triangle you arrive at 4, you will move right to the 3, giving a sum of 7 (plus whatever is in the path above it). If you’ve reached 1, you will move left to the 3, giving a sum of 4 (plus whatever is in the path above it). If you’re at 2, you can move either way for a sum of 3 (plus whatever is in the path above it). Thus, by replacing the next-to-last row with the sums, the triangle

      1
     1 2
    7 4 3
    

    will have the same maximum-sum path as the original triangle. Now do the same process recursively on the reduced triangle. From 1 on the next-to-last row move left to 7, giving a sum of 8, and from 2 move left to 4, giving a sum of 6. The reduced triangle now looks like

     1
    8 6
    

    Finally, from 1 on the next-to-last row move left to 8, giving a sum of 9, which is the answer to the problem.

    There is also a method of working from the top down. At each step you replace each number in the triangle with the maximum-sum of any path leading to that number. Starting from the top, the triangle starts

    1
    

    Then the second row is replaced by its sums

     1
    2 3
    

    Then the third row

      1
     2 3
    6 4 5
    

    And finally the fourth row

       1
      2 3
     6 4 5
    8 9 6 6
    

    The answer is the largest sum in the bottom row, which is 9. I’ve always found the top-down approach harder to manage than the bottom-up approach, but the two algorithms are dual to each other, so it’s your choice which to implement. The top-down approach does have the advantage that you can accumulate the next row as you’re reading the data; with the bottom-up approach, you have to read and store the entire input before you compute any of the sums.

    I’ll leave it to you to write the code. When you do, remember that you only need to store two rows at a time, the previous row and the next row. Which is previous and which is next depends on whether you’re working top-down or bottom-up — the previous row is the row you just filled in and the next row is the row you’re currently working on, which means that if you’re working top-down the next row has one more sum than the previous row, and if you’re working bottom-up the next row has one less sum than the previous row. Please post your code when you get it working, so others can learn from your effort.

    By the way, this problem originally comes from Project Euler. Code Chef stole it from them, apparently without attribution, which really isn’t a very nice thing to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I'd like to know if this code is okay, <body id=some_id <!--[if lt
Basically I'd be looking to implement a method like this. IQueryAble GetQuery<T>(Entities db) or
Basically from a database I am getting data that is formatted like this nameofproject101
Basically I have a few functions that look like this: class MyClass { void
basically i have (state, state code) pairs, that are subsets of country [USA] ->
Basically I would like to store the address of a pointer in a buffer.
Basically, I would like to decode a given HTML document, and replace all special
Basically, what I'm trying to create is a page of div tags, each has
Basically I'm attempting to add rows to a table, I need to do this
Basically what I'm trying to do is make a very simple vertical bullet projectile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.