Haskell Quiz/Maximum Sub-Array/Solution Jkramar: Difference between revisions
No edit summary |
mNo edit summary |
||
Line 3: | Line 3: | ||
<haskell> | <haskell> | ||
maxSubArray xs = drop from$take to xs where | maxSubArray xs = drop from$take to xs where | ||
sumswithpos = zip (scanl (+) 0 xs) [0..] | |||
diff ((a,ai),(b,bi)) = (a-b,(bi,ai)) | diff ((a,ai),(b,bi)) = (a-b,(bi,ai)) | ||
(from,to) = snd$maximum$map diff$zip | (from,to) = snd$maximum$map diff$zip sumswithpos$scanl1 min sumswithpos | ||
</haskell> | </haskell> |
Revision as of 20:01, 19 November 2008
maxSubArray xs = drop from$take to xs where
sumswithpos = zip (scanl (+) 0 xs) [0..]
diff ((a,ai),(b,bi)) = (a-b,(bi,ai))
(from,to) = snd$maximum$map diff$zip sumswithpos$scanl1 min sumswithpos