Last active
December 25, 2015 10:09
-
-
Save jeffcarp/6959725 to your computer and use it in GitHub Desktop.
Max Consecutive Sum (problem of the week on http://codingforinterviews.com)
This file contains hidden or 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
(defun max_consecutive_sum (arr) | |
(let ((max_ending_here 0) (max_so_far 0)) | |
(dotimes (x (length arr)) | |
(setf max_ending_here (max 0 (+ (aref arr x) max_ending_here))) | |
(setf max_so_far (max max_so_far max_ending_here))) | |
max_so_far)) | |
(format t "~S~%" (max_consecutive_sum #(-1 5 6 -2 20 -50 4))) ; 29 | |
(format t "~S~%" (max_consecutive_sum #(-2 1 -3 4 -1 2 1 -5 4))) ; 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment