Created
March 20, 2017 15:54
-
-
Save loschtreality/2e10ddf89b195774ef1b2650130bf2b5 to your computer and use it in GitHub Desktop.
Max slice with merge
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
const solution = (array) => { | |
if (array.length === 0) return 0 | |
const midPoint = Math.floor(array.length / 2) | |
const left = array.slice(1, midPoint - 1) | |
const right = array.slice(midPoint, array.length - 2) | |
const leftSum = left.reduce((sum, current) => { return sum + current }, 0) | |
const rightSum = right.reduce((sum, current) => { return sum + current }, 0) | |
return Math.max(leftSum + rightSum, solution(left) + solution(right)) | |
} | |
const array = [3, 2, 6, -1, 4, 5, -1, 2] | |
// const array = [5, -2, 10, 3, -25, 12, 6] | |
// console.log(slowSolution(array)) // expect 17, got: 17 | |
console.log(solution(array)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment