Created
October 24, 2018 00:54
-
-
Save basekays/90df32c7bede71f2892a613ee4c125ce to your computer and use it in GitHub Desktop.
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
function maxRangeSum(string) { | |
const splitString = string.split(" "); | |
splitString.shift(); | |
const gainsLossesArray = []; | |
for (let i = 0; i < splitString.length; i++) { | |
gainsLossesArray.push(parseInt(splitString[i])); | |
} | |
let currentMax = 0; | |
let maxGain = 0; | |
for (let i = 0; i < gainsLossesArray.length; i++) { | |
currentMax = Math.max(0, currentMax + gainsLossesArray[i]); | |
maxGain = Math.max(maxGain, currentMax); | |
} | |
return maxGain; | |
} | |
maxRangeSum('10 7 -3 -10 4 2 8 -2 4 -5 -6'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment