Skip to content

Instantly share code, notes, and snippets.

@pbostrom
Created October 10, 2013 22:20
Show Gist options
  • Save pbostrom/6926519 to your computer and use it in GitHub Desktop.
Save pbostrom/6926519 to your computer and use it in GitHub Desktop.
max sum problem
(defn f [{:keys [mx acc]} x]
{:mx (max mx (+ acc x))
:acc (max 0 (+ acc x))})
(defn max-sum [v]
(:mx (reduce f {:mx 0 :acc 0} v)))
(max-sum [-1, 5, 6, -2, 20, -50, 4]) ;29
(max-sum [-1, 5, 6, -2, 21, -50, 4, -100, 30, -100, 5, 8, -2, 20, -50, 4]) ;31
(max-sum [-2, 1, -3, 4, -1, 2, 1, -5, 4]) ;6
(max-sum [-2, -3, -1, -5]) ;0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment