Greatest subsequential sum: Difference between revisions
Content added Content deleted
(→Functional Python: pylinted for Python 3, added {Works with} tag) |
m (→{{header|Haskell}}: Reduced two nested `where` expressions to a single `let in` with a `<*>`) |
||
Line 1,433: | Line 1,433: | ||
Secondly, the linear time constant space approach: |
Secondly, the linear time constant space approach: |
||
<lang haskell>maxSubseq :: [Int] -> (Int, [Int]) |
<lang haskell>maxSubseq :: [Int] -> (Int, [Int]) |
||
maxSubseq = |
maxSubseq = |
||
let go x ((h1, h2), sofar) = |
|||
where |
|||
((,) <*> max sofar) (max (0, []) (h1 + x, x : h2)) |
|||
in snd . foldr go ((0, []), (0, [])) |
|||
where |
|||
high = max (0, []) (h1 + x, x : h2) |
|||
main :: IO () |
main :: IO () |