Created
October 10, 2013 22:20
-
-
Save pbostrom/6926519 to your computer and use it in GitHub Desktop.
max sum problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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