Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Think of your pyramid as a tree with the root at the top of the pyramid: I think you want the route with the maximum cost from the root to any of the leaf nodes (bottom of the pyramid). OK, it's not actually a tree, because a node may have two parents, in fact you can get to a node at level <code>i</code> from at most two nodes at level <code>i-1</code>.</p> <p>Anyway, I think you can compute the route with the maximum cost by using dynamic programming. Let me rewrite your data in a matrix like way:</p> <pre><code>7 4 8 1 8 9 2 4 6 7 4 6 7 4 9 4 9 7 3 8 8 </code></pre> <p>and let the missing elements of the matrix be 0. Let's call this matrix <code>v</code> (for values). Now you can build a matrix <code>c</code> (for costs) where <code>c(i,j)</code> is the maximum cost for reaching the tree node at position <code>(i,j)</code>. You can compute it with this recurrence:</p> <p><code>c(i,j) = v(i,j) + max{ c(i-1,j-1), c(i-1,j) }</code></p> <p>where <code>c(h,k)</code> is 0 when you get to a position out of the matrix. Essentially we say that the maximum cost for getting to node at position <code>(i,j)</code> is the cost of the node itself plus the maximum between the costs of getting to its two possible parents at level <code>i-1</code>.</p> <p>Here is <code>c</code> for your example:</p> <pre><code>7 11 15 12 23 24 14 27 30 31 18 33 37 35 40 22 42 44 40 48 48 </code></pre> <p>For instance, let's take <code>i=3, j=2</code>: </p> <pre><code>c(3,2) = v(3,2) + max{ c(2,1), c(2,2) } = 6 + max{ 23 , 24 } = 30 </code></pre> <p>From <code>c</code> you see that the most expensive rout costs 48 (and you have two of them).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload