Created
October 28, 2018 18:24
-
-
Save renatoargh/58c14c7019311781145d8cf68069e66d to your computer and use it in GitHub Desktop.
sum: O(n), get sum of segment: O(1)
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
function prefixedSum(array) { | |
const sum = new Array(array.length + 1) | |
sum[0] = 0 | |
for(let k = 1; k < sum.length; k++) { | |
sum[k] = (sum[k - 1] || 0) + array[k - 1] | |
} | |
return sum | |
} | |
function segmentSum(sum, x, y) { | |
return sum[y + 1] - sum[x] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment