Created
September 16, 2016 15:31
-
-
Save anonymous/4fd2f07abd0148941766db41f78dcad0 to your computer and use it in GitHub Desktop.
TryCF Gist
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
<cfscript> | |
numeric function maxSequence(required array a){ | |
var maxSum = 0; | |
var maxSubset = []; | |
var currentSum = 0; | |
var currentSubset = []; | |
for( var n in arguments.a ){ | |
var newSum = currentSum + n; | |
if( newSum <= 0 ){ | |
currentSum = 0; | |
currentSubset = []; | |
continue; | |
} | |
currentSum = newSum; | |
currentSubset.Append(n); | |
if( currentSum > maxSum ){ | |
maxSum = currentSum; | |
maxSubset = currentSubset; | |
} | |
} | |
return maxSum; | |
} | |
WriteDump(maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])); | |
WriteDump(maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4, 3])); | |
WriteDump(maxSequence([-2, -3, -1, -5])); | |
WriteDump(maxSequence([1, 4, 2, 1, 4, 3])); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment