Created
June 24, 2022 09:46
-
-
Save bendozy/c6ab1aae558877da137a2c5963810ccd to your computer and use it in GitHub Desktop.
Array Manipulation - Hackerrank
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 arrayManipulation(n: number, queries: number[][]): number { | |
const data: number[] = Array(n).fill(0); | |
let maxValue: number = Number.MIN_VALUE; | |
let sum: number = 0 | |
queries.forEach(([startIndex, endIndex, fillValue]) => { | |
data[startIndex - 1] += fillValue | |
if(data[endIndex] !== undefined) data[endIndex] -= fillValue | |
}) | |
data.forEach((value) => { | |
sum += value | |
maxValue = Math.max(sum, maxValue) | |
}) | |
return maxValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment