Maximum triangle path sum: Difference between revisions
m
→{{header|Sidef}}: replaced operator "..." with "..^"
m (→{{header|REXX}}: added/changed whitespace and comments, simplified program, changed indentations.) |
m (→{{header|Sidef}}: replaced operator "..." with "..^") |
||
Line 1,686:
Iterative solution:
<lang ruby>var sum = [0]
ARGF.each { |line|
var x = line.words.map{.to_i}
sum = [
x.first + sum.first,
1 ..
x.last + sum.last,
]
}
say sum.max
Recursive solution:
<lang ruby>var triangle = ARGF.slurp.lines.map{.words.map{.to_i}}
func max_value(i=0, j=0) is cached {
i == triangle.len && return 0
triangle[i][j] + [max_value(i+1, j), max_value(i+1, j+1)].max
}
say max_value()
{{out}}
<pre>% sidef maxpath.sf triangle.txt
|