2018-03-25 21:12:43 -04:00
|
|
|
# Find the maximum path from the top (root)
|
|
|
|
# of the triangle to the leaves of the triangle.
|
2018-08-22 21:41:25 -04:00
|
|
|
|
2018-03-25 18:51:31 -04:00
|
|
|
(defn myfold [xs ys]
|
2019-03-21 14:32:08 -04:00
|
|
|
(let [m1 (map + [;xs 0] ys)
|
|
|
|
m2 (map + [0 ;xs] ys)]
|
2018-05-08 19:40:28 -04:00
|
|
|
(map max m1 m2)))
|
2018-03-25 18:51:31 -04:00
|
|
|
|
|
|
|
(defn maxpath [t]
|
2018-08-22 21:41:25 -04:00
|
|
|
(extreme > (reduce myfold () t)))
|
2018-04-01 15:08:51 -04:00
|
|
|
|
|
|
|
# Test it
|
|
|
|
# Maximum path is 3 -> 10 -> 3 -> 9 for a total of 25
|
2018-03-25 18:51:31 -04:00
|
|
|
|
2018-08-22 21:41:25 -04:00
|
|
|
(def triangle
|
|
|
|
'[[3]
|
|
|
|
[7 10]
|
|
|
|
[4 3 7]
|
2018-11-26 22:09:12 -05:00
|
|
|
[8 9 1 3]])
|
2018-03-25 18:51:31 -04:00
|
|
|
|
|
|
|
(print (maxpath triangle))
|