Maximum triangle path sum: Difference between revisions

m
m (J: eliminate some unnecessary decoration)
Line 461:
 
=={{header|J}}==
<lang j>padTri=: 0 ". ];._2 NB. parse triangle and (implicitly) pad with zeros
maxSum=: [: {. (+ (0 ,~ 2 >./\ ]))/ NB. find max triangle path sum</lang>
 
Line 473:
 
Second, starting with the last row, for each pair of numbers we find the largest of the two (resulting in a list slightly shorter than before, so of course we pad it with a trailing zero) and add that row to the previous row. After repeating this through all the rows, the first value of the resulting row is the maximum we were looking for.
 
Instead of padding, we could instead trim the other argument to match the current reduced row length.
 
<lang J>maxsum=: ((] + #@] {. [)2 >./\ ])/</lang>
 
However, this turns out to be a slightly slower approach, because we are doing a little more work for each row.
 
=={{header|Java}}==
6,962

edits